티스토리 뷰

1. application.properties 값 조회  

<span th:text="${@environment.getProperty('app.title')}"></span> 


2. message 조회 

<span th:text="#{msg.example.title}"></span>


3. 세션 정보 조회

<span th:text="${session['userId']}"></span> 

<span th:text="${session.userId}"></span> 


4. Parameter 정보 조회

<span th:text="${param.authType}"></span> 

<span th:text="${#httpServletRequest.getParameter('authType')}"></span> 


5. PathVariable 가져오기 

<span th:text="__${userId}__"></span>

* Spring 컨트롤러에 Request Mapping에 선언되어 있고 @PathVariable이 있어야만 정보를 가져올 수 있다.


예제)

application.properties 

app.title=thymeleaf test project


message.properties

msg.example.title=메시지 가져 오기 테스트


요청 URL 

http://localhost:8080/users/1234?authType=facebook


컨트롤러 
1
2
3
4
5
6
7
8
9
10
11
12
13
@Controller
public class UserTestController {
    @GetMapping("/users/{userId}")
    public String getUserList(@PathVariable String userId, User pUser, HttpSession session, Model model) {
        session.setAttribute("userId", pUser.getUserId());
        
        pUser.setName("테스터");
        pUser.setAuthType("facebook");
        model.addAttribute("user", pUser);
        return "user";
    }
}
 
cs


html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Thymeleaf 예제</title>
    <script th:src="@{/assets/vendor/jquery/jquery.js}"></script>
</head>
<body>
<h1>application.properties 값 조회</h1>
app title : <span th:text="${@environment.getProperty('app.title')}"></span><br/>
 
<h1>message 조회</h1>
msg.example.title : <span th:text="#{msg.example.title}"></span><br/>
 
<h1>세션 조회</h1>
session['userId'] : <span th:text="${session['userId']}"></span><br/>
session.userId : <span th:text="${session.userId}"></span><br/>
 
<h1>Parameter 가져오기 </h1>
{authType} : <span th:text="${param.authType}"></span><br/>
{authType} : <span th:text="${#httpServletRequest.getParameter('authType')}"></span><br/>
 
<h1>PathVariable 가져오기 </h1>
{userId} : <span th:text="__${userId}__"></span><br/>
 
</body>
</html>
cs


결과 화면

application.properties 값 조회

app title : thymeleaf test project

message 조회

msg.example.title : 메시지 가져 오기 테스트

세션 조회

session['userId'] : 1234
session.userId : 1234

Parameter 가져오기

{authType} : facebook
{authType} : facebook

PathVariable 가져오기

{userId} : 1234


댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함