10-2. Thymeleaf

๋™๋™์ฃผยท2024๋…„ 6์›” 8์ผ
0

spring ๊ธฐ๋ง๊ณ ์‚ฌ

๋ชฉ๋ก ๋ณด๊ธฐ
6/11

๐Ÿฅ  ์†์„ฑ ๊ฐ’ ์„ค์ •

1) ์ž„์˜์˜ ์†์„ฑ ์„ค์ •

  • ํ˜•์‹: th:attr="attribute-name=expr"
 <input type="submit" value="Subscribe!" th:attr="value=#{subscribe.submit}"/>
 <input type="submit" value="๊ตฌ๋…!" /> // #{subscribe.submit}์˜ ๊ฐ’์ด "๊ตฌ๋…!"์ธ ๊ฒฝ์šฐ

2) ํŠน์ • ์†์„ฑ ์„ค์ •

  • ํ˜•์‹: th:attribute-name="expr"
 <input type="submit" value="Subscribe!" th:value="#{subscribe.submit}"/>
 <input type="submit" value="๊ตฌ๋…!" /> 

3) Fixed-value boolean attributes

  • ์‹์˜ ๊ฒฐ๊ณผ๊ฐ€ true์ด๋ฉด ๊ณ ์ •๊ฐ’์„ ์„ค์ •ํ•˜๊ณ  false์ด๋ฉด ์†์„ฑ ์ž์ฒด๋ฅผ ์ƒ๋žต
    • th:checked, th:selected, th:readonly, th:required, th:hidden, th:disabled
 <input type="checkbox" name="active" th:checked="${user.active}" />
 // ${user.active} == true ์ด๋ฉด checked="checked" ์†์„ฑ ์ถ”๊ฐ€

4) th:attrappend, th:attrprepend

  • ๊ธฐ์กด ์†์„ฑ ๊ฐ’์— ์ƒˆ๋กœ์šด ๊ฐ’์„ appendํ•˜๊ฑฐ๋‚˜ prependํ•จ
<input type="button" value="Do it!" class="btn" th:attrappend="class=${' ' + cssStyle}" />
<input type="button" value="Do it!" class="btn" th:classappend="${' ' + cssStyle}">
 // ${cssStyle} == โ€˜warningโ€™ ์ผ ๋•Œ, class="btn warning" ์†์„ฑ ์ƒ์„ฑ

๐Ÿฅ  ๋ฐ˜๋ณต๋ฌธ(Iteration)

th:each

User ๊ฐ์ฒด๊ฐ€ 3๊ฐœ ์ €์žฅ๋œ List ์ปฌ๋ ‰์…˜์˜ ๋ฐ˜๋ณต

<tr th:each="user : ${users}">
    <td th:text="${user.username}">username</td>
    <td th:text="${user.age}">0</td>
</tr>

status ๋ณ€์ˆ˜

  • index : 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” index
  • count : 1๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” index
  • size : ์ด ๊ฐœ์ˆ˜
  • current : ํ˜„์žฌ index์˜ ๋ณ€์ˆ˜
  • even odd : ์ง์ˆ˜/ํ™€์ˆ˜ ์—ฌ๋ถ€
  • first last : ์ฒ˜์Œ/๋งˆ์ง€๋ง‰ ์—ฌ๋ถ€

๐Ÿ’ฆ count ์˜ˆ์‹œ

<th:block th:each="data,status:${datas}">
	<h1 th:text="|${status.count} ${data}|"></h1>
</th:block>
  • ๊ฒฐ๊ณผ
<h1>1 apple</h1>
<h1>2 banana</h1>
<h1>3 cherry</h1>

๐Ÿ’ฆ odd ์˜ˆ์‹œ

 <tr th:each="prod,iterStat : ${prods}" th:class="${iterStat.odd} ? 'odd'">
  • ํ™€์ˆ˜ ๋ฒˆ์งธ ๋ฐ˜๋ณต(ํ™€์ˆ˜ ๋ฒˆ์งธ <tr>)์— ๋Œ€ํ•ด์„œ๋งŒ class="odd" ์†์„ฑ์„ ์ƒ์„ฑ

๐Ÿฅ  ์กฐ๊ฑด๋ฌธ(Conditional Evaluation)

th:if, th:unless

<div th:unless="${session.authInfo}">   
<!โ€“session์— authInfo๊ฐ์ฒด๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ(null), ํ•˜์œ„ ์ฝ”๋“œ ์‹คํ–‰--> 
...

<div th:if="${session.authInfo}">
<!โ€“session์— authInfo๊ฐ์ฒด๊ฐ€ ์กด์žฌํ•  ๊ฒฝ์šฐ, ํ•˜์œ„ ์ฝ”๋“œ ์‹คํ–‰-->
...
  • ์‹์˜ ๊ฐ’์ด null์ด๋ฉด false, null์ด ์•„๋‹ˆ๋ฉด ๋‹ค์Œ์˜ ๊ฒฝ์šฐ true

๐Ÿง ํ—ท๊ฐˆ๋ฆฌ๋Š” ์ฝ”๋“œ

<table th:unless="${#lists.isEmpty(members)}"> 
<!-- th:if="${not#lists.isEmpty(members)}" ์™€ ๋™์ผ-->

th:switch, th:case

<div th:switch="${user.role}">
<p th:case="'adminโ€™โ€>an administrator</p>           
             // ${user.role}==โ€˜adminโ€™์ผ ๋•Œ
<p th:case=โ€œ#{roles.manager}โ€>a manager</p>  
            // ${user.role}==#{roles.manager}์ผ ๋•Œ
<p th:case=โ€œ*โ€>some other thing</p>                     
            // ๊ทธ ์™ธ(default)
 </div>


๐Ÿฅฐ ์ถœ์ฒ˜: https://jddng.tistory.com/233 [IT ๊ฐœ๋ฐœ์ž๋“ค์˜ ์šธํƒ€๋ฆฌ:ํ‹ฐ์Šคํ† ๋ฆฌ] https://velog.io/@mon99745/Thymeleaf-%EC%B4%9D-%EC%A0%95%EB%A6%AC

0๊ฐœ์˜ ๋Œ“๊ธ€