<ul><li><ath:href="@{/hello}">basic url</a></li><li><ath:href="@{/hello(param1=${param1}, param2=${param2})}">hello query param</a></li><li><ath:href="@{/hello/{param1}/{param2}(param1=${param1}, param2=${param2})}">path variable</a></li><li><ath:href="@{/hello/{param1}(param1=${param1}, param2=${param2})}">path variable + query parameter</a></li></ul><!--굳이 파람을 가져올 필요가 없다면 'test' 처럼 직접 넣어도 됩니다.-->
th:href="@{/basic/items/{itemId}(itemId=${item.id}, query='test')}"
<!-- 리터럴 대체 문법 응용 -->
th:href="@{|/basic/items/${item.id}|}"
<!-- 리터럴 사용하겠다고 먼저 || 를 사용하고 그 안에서 url 경로식 있으면 그 안에 다시 || 써줘야합니다. -->
th:onclick="|location.href='@{|/basic/items/${item.id}/edit|}'|"
리터럴
리터털이란, 소스 코드에서 고정된 값을 말한다.
Thymeleaf에서 리터럴을 사용하고자 한다면 항상 '(작은 따옴표)로 감싸야 한다.
리터럴 대체 문법으로 ||안에 문자열을 작성하면 하나의 의미 있는 토큰으로 인식하여 작은 따옴표를 사용하지 않아도 된다.
<!-- 원래는 작은 따옴표로 감싸야함--><spanth:text="'hello'"><!-- 붙어있으면 생략 가능--><spanth:text="hello"><!-- 붙어있지 않으므로 작은 따옴표 필수--><spanth:text="'hello world'"><!-- 덧셈 연산 가능--><spanth:text="'hello' + ' world'"><!-- 리터럴 대체 문법 ||--><spanth:text="|hello ${data}|"></span>
<!-- name 태그를 th:name의 태그로 대체해서 랜더링--><inputtype="text" name="mock" th:name="userA" /><!-- class 태그 뒤쪽에 large를 붙여서 랜더링 -> text large --><inputtype="text" class="text" th:classappend="large" /><br/><!-- html은 check 속성이 있으면 true, false에 관계없이 그냥 체크박스에 체크를 해버림 --><inputtype="checkbox" name="active" checked="false" /><br/><!-- 그에 반해 타임리프는 값이 적용된다. --><inputtype="checkbox" name="active" th:checked="false" /><br/>
<!-- 자바스크립트 인라인 사용 후 --><scriptth:inline="javascript">varusername=[[${user.username}]];varage=[[${user.age}]];//자바스크립트 내추럴 템플릿varusername2=/*[[${user.username}]]*/"test username";//객체varuser=[[${user}]];</script>
<!DOCTYPE html><htmlxmlns:th="http://www.thymeleaf.org"><body><footerth:fragment="copy">
푸터 자리 입니다.
</footer><footerth:fragment="copyParam (param1, param2)"><p>파라미터 자리 입니다.</p><pth:text="${param1}"></p><pth:text="${param2}"></p></footer></body></html>
<!DOCTYPE html><htmlxmlns:th="http://www.thymeleaf.org"><head><metacharset="UTF-8"><title>Title</title></head><body><h1>부분 포함</h1><h2>부분 포함 insert</h2><divth:insert="~{template/fragment/footer :: copy}"></div><h2>부분 포함 replace</h2><divth:replace="~{template/fragment/footer :: copy}"></div><h2>부분 포함 단순 표현식</h2><divth:replace="template/fragment/footer :: copy"></div><h1>파라미터 사용</h1><divth:replace="~{template/fragment/footer :: copyParam ('데이터1', '데이터2')}"></div></body></html>
템플릿 레이아웃
<htmlxmlns:th="http://www.thymeleaf.org"><headth:fragment="common_header(title,links)"><titleth:replace="${title}">레이아웃 타이틀</title><!-- 공통 --><linkrel="stylesheet" type="text/css" media="all" th:href="@{/css/awesomeapp.css}"><linkrel="shortcut icon" th:href="@{/images/favicon.ico}"><scripttype="text/javascript" th:src="@{/sh/scripts/codebase.js}"></script><!-- 추가 --><th:blockth:replace="${links}" /></head>
<!DOCTYPE html><htmlxmlns:th="http://www.thymeleaf.org"><!-- 이 부분이 핵심 --><headth:replace="template/layout/base :: common_header(~{::title},~{::link})"><title>메인 타이틀</title><linkrel="stylesheet" th:href="@{/css/bootstrap.min.css}"><linkrel="stylesheet" th:href="@{/themes/smoothness/jquery-ui.css}"></head><body>
메인 컨텐츠
</body></html>