타임리프에서 URL을 생성할 때는 @{...} 문법을 사용하면 된다.
BasicController
@GetMapping("/link")
public String link(Model model){
model.addAttribute("param1", "data1");
model.addAttribute("param2", "data2");
return "basic/link";
}
basic/link.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>URL 링크</h1>
<ul>
<li><a th:href="@{/hello}">basic url</a></li>
<li><a th:href="@{/hello(param1=${param1}, param2=${param2})}">hello query param</a></li>
<li><a th:href="@{/hello/{param1}/{param2}(param1=${param1}, param2=${param2})}">path variable</a></li>
<li><a th:href="@{/hello/{param1}(param1=${param1}, param2=${param2})}">path variable + query parameter</a></li>
</ul>
</body>
</html>
위의 코드는 타임리프에서 URL을 사용하는 방법 예시이다.
코드를 참조하여, 아래의 설명을 확인하자.
단순 URL
- @{/hello} -> /hello
쿼리 파라미터
- @{/hello{param1=${param1}, param2=${param2})}
- -> /hello?param1=data1¶m2=data2
- ()에 있는 부분은 쿼리 파라미터로 처리된다.
경로 변수
- @{/hello/{param1}/{param2}(param1=${param1}, param2=${param2})}
- -> /hello/data1/data2
- URL 경로상에 변수가 있으면 () 부분은 경로 변수로 처리된다.
경로 변수 + 쿼리 파라미터
- @{hello/{param1}(param1=${param1}, param2=${param2})}
- -> /hello/data1?param2=data2
- 경로 변수와 쿼리 파라미터를 함께 사용할 수 있다.
상대경로, 절대경로, 프로토콜 기준을 표현할 수도 있다.
- /hello : 절대 경로
- hello : 상대 경로
보다 자세한 내용은 아래의 공식문서를 참고하자.
https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#link-urls
Tutorial: Using Thymeleaf
1 Introducing Thymeleaf 1.1 What is Thymeleaf? Thymeleaf is a modern server-side Java template engine for both web and standalone environments, capable of processing HTML, XML, JavaScript, CSS and even plain text. The main goal of Thymeleaf is to provide a
www.thymeleaf.org
'Etc > Thymeleaf' 카테고리의 다른 글
Thymeleaf - 연산 (0) | 2023.07.25 |
---|---|
Thymeleaf - 리터럴(Literals) (0) | 2023.07.25 |
Thymeleaf - 유틸리티 객체와 날짜 (0) | 2023.07.24 |
Thymeleaf - 기본 객체 (0) | 2023.07.24 |
Thymeleaf - 변수(SpringEL) (0) | 2023.07.24 |