타임리프(Thymeleaf) - 기본기능3

SeungTaek·2021년 8월 14일
0

타임리프(Thymeleaf)

목록 보기
3/6
post-thumbnail

본 게시물은 스스로의 공부를 위한 글입니다.
틀린 내용이 있을 수 있습니다.

📌연산

  • 자바와 크게 다르진 않지만, HTML 엔티티를 사용해야 한다는것만 주의하자.

  • 산술 연산

    • 10+2
    • 10 % 2=0
  • 비교연산

    • 1 > 10
    • 1 gt 10
    • 1 >= 10
    • 1 ge 10
    • 1==10
    • 1!=10
  • 조건식

    • <span th:text="(10 % 2 == 0)? '짝수':'홀수'"></span>
  • Elvis 연산자

    • if의 축약형이다. data가 있으면 data출력, 없으면 ?:의 왼쪽이 출력된다.
    • <span th:text="${data}?: '데이터가 없습니다.'"></span>
    • <span th:text="${nullData}?: '데이터가 없습니다.'"></span>
  • No-Operation

    • _인 경우 마지 타임리프가 없는 것처럼 동작한다.
    • <span th:text="${data}?: _">
      -> data가 있으면 출력, 없으면 타임리프가 없는 <span> </span> 으로 작동

📌속성 값 설정

  • 타임리프는 주로 HTML 태그에 th:* 속성을 지정하는 방식으로 동작한다

  • th:* 로 속성을 적용하면 기존 속성을 대체한다. 기존 속성이 없으면 새로 만든다.

  • th:attrappend : 속성 값의 뒤에 값을 추가한다. (attribute append 줄인말)
    th:attrprepend : 속성 값의 앞에 값을 추가한다.
    th:classappend : class 속성에 자연스럽게 추가한다.

    • 아래 예시에서 large주변에 띄어쓰기가 있음을 주의하자. 없으면 textlarge처럼 클래스 속성이 서로 붙어버린다.

    • <input type="text" class="text" th:attrappend="class=' large'"/>

    • <input type="text" class="text" th:attrprepend="class='large '"/>

    • <input type="text" class="text" th:classappend="large"/>

    • 주로 classappend를 사용한다. (띄어쓰기를 고려하지 않아도 괜찮기 때문이다.)

  • checked처리 (In checkbox)

    • html에선 checked 속성이 존재만 하면 그냥 체크되어버린다. (checked=false라도 말이다.)

    • 즉, 체크를 안하려면 속성 자체가 없어야 된는데.. true, false를 다루는 개발자 입장에서는 불편하다.

    • 그래서 타임리프는 th:checked 의 값이 false 인 경우 checked 속성 자체를 제거한다

      • <input type="checkbox" name="active" th:checked="false" />
      • -> <input type="checkbox" name="active" />

📌반복

  • 타임리프에서 반복은 th:each 를 사용한다. 추가로 반복에서 사용할 수 있는 여러 상태 값을 지원한다.

  • th:each가 있는 태그부터 내부 태그까지 통째로 반복된다.

  • 반복의 두번째 파라미터를 설정해서 반복의 상태를 확인 할 수 있다. 두번째 파라미터는 생략 가능한데, 생략하면 지정한 변수명( user ) + Stat으로 사용이 가능하다. 아래에서는 user + Stat = userStat 이므로 생략이 가능하다.

  • 반복 상태 유지 기능
    index : 0부터 시작하는 값
    count : 1부터 시작하는 값
    size : 전체 사이즈
    even , odd : 홀수, 짝수 여부( boolean )
    first , last :처음, 마지막 여부( boolean )
    current: 현재 객체

<tr th:each="user : ${users}">
	<td th:text="${user.username}">username</td>
 	<td th:text="${user.age}">0</td>
</tr>
<tr th:each="user, userStat : ${users}">
 <td th:text="${userStat.count}">username</td>
 <td th:text="${user.username}">username</td>
 <td th:text="${user.age}">0</td>
 <td>
 	index = <span th:text="${userStat.index}"></span>
        count = <span th:text="${userStat.count}"></span>
	size = <span th:text="${userStat.size}"></span>
 	even? = <span th:text="${userStat.even}"></span>
 	odd? = <span th:text="${userStat.odd}"></span>
 	first? = <span th:text="${userStat.first}"></span>
 	last? = <span th:text="${userStat.last}"></span>
 	current = <span th:text="${userStat.current}"></span>
 </td>
</tr>
  • List 뿐만 아니라 배열, java.util.Iterable , java.util.Enumeration 을 구현한 모든
    객체를 반복에 사용할 수 있다.
  • Mapth:each를 이용할 수 있는데, 이 경우 변수에 담기는 값은 Map.Entry이다.

인프런의 '스프링 MVC 2편(김영한)'을 스스로 정리한 글입니다.
자세한 내용은 해당 강의를 참고해주세요.

profile
I Think So!

0개의 댓글