BackEnd/Thymeleaf

[Thymeleaf] 유틸리티 객체와 날짜

샤아이인 2022. 1. 26.

인프런 김영한님의 Spring강의에서 공부한것을 올리며, Thymeleaf의 경우 unit 단위로 공부후 각각 정리하는 글을 작성하겠습니다.

[Thymeleaf] 유틸리티 객체와 날짜

 

유틸리티 객체와 날짜

타임리프는 문자, 숫자, 날짜, URI등을 편리하게 다루기 위한 유틸리티 객체들을 제공한다.

 

● 타임리프 유틸리티 객체들

#message : 메시지, 국제화 처리

#uris : URI 이스케이프 지원

#dates : java.util.Date 서식 지원

#calendars : java.util.Calendar 서식 지원

#temporals : 자바8 날짜 서식 지원

#numbers : 숫자 서식 지원

#strings : 문자 관련 편의 기능

#objects : 객체 관련 기능 제공

#bools : boolean 관련 기능 제공

#arrays : 배열 관련 기능 제공

#lists , #sets , #maps : 컬렉션 관련 기능 제공

#ids : 아이디 처리 관련 기능 제공, 뒤에서 설명

 

위와 같은 유틸리티 객체들을 제공하는데, 우선 이런게 있다는것만 알아두고 필요할때 찾아보자!

 

사용 예시는 다음 링크에서 확인 가능하다.

● 자바8 날짜

타임리프에서 자바8 날짜인 LocalDate , LocalDateTime , Instant 를 사용하려면 추가 라이브러리인 thymeleaf-extras-java8time 가 필요하다. 스프링 부트에서 타임리프를 사용하면 해당 라이브러리가 자동으로 추가해준다.

 

따라서 타임리프에서 자바8 날짜용 유틸리티는 #temporals 로 사용하면 된다. 다음 코드와 같이 말이다.

<span th:text="${#temporals.format(localDateTime, 'yyyy-MM-dd HH:mm:ss')}"></span>
 

날짜 정보를 넘기는 간단한 컨트롤러를 만들어보자.

@GetMapping("/date")
public String date(Model model) {
    model.addAttribute("localDateTime", LocalDateTime.now());
    return "basic/date";
}
 

이를 처리할 뷰는 다음과 같다.

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<h1>LocalDateTime</h1>
<ul>
    <li>default = <span th:text="${localDateTime}"></span></li>
    <li>yyyy-MM-dd HH:mm:ss = <span th:text="${#temporals.format(localDateTime, 'yyyy-MM-dd HH:mm:ss')}"></span></li>
</ul>

<h1>LocalDateTime - Utils</h1>
<ul>
    <li>${#temporals.day(localDateTime)} = <span th:text="${#temporals.day(localDateTime)}"></span></li>
    <li>${#temporals.month(localDateTime)} = <span th:text="${#temporals.month(localDateTime)}"></span></li>
    <li>${#temporals.monthName(localDateTime)} = <span th:text="${#temporals.monthName(localDateTime)}"></span></li>
    <li>${#temporals.monthNameShort(localDateTime)} = <span th:text="${#temporals.monthNameShort(localDateTime)}"></span></li>
    <li>${#temporals.year(localDateTime)} = <span th:text="${#temporals.year(localDateTime)}"></span></li>
    <li>${#temporals.dayOfWeek(localDateTime)} = <span th:text="${#temporals.dayOfWeek(localDateTime)}"></span></li>
    <li>${#temporals.dayOfWeekName(localDateTime)} = <span th:text="${#temporals.dayOfWeekName(localDateTime)}"></span></li>
    <li>${#temporals.dayOfWeekNameShort(localDateTime)} = <span th:text="${#temporals.dayOfWeekNameShort(localDateTime)}"></span></li>
    <li>${#temporals.hour(localDateTime)} = <span th:text="${#temporals.hour(localDateTime)}"></span></li>
    <li>${#temporals.minute(localDateTime)} = <span th:text="${#temporals.minute(localDateTime)}"></span></li>
    <li>${#temporals.second(localDateTime)} = <span th:text="${#temporals.second(localDateTime)}"></span></li>
    <li>${#temporals.nanosecond(localDateTime)} = <span th:text="${#temporals.nanosecond(localDateTime)}"></span></li>
</ul>

</body>
</html>
 

실행 결과는 다음과 같다.

[Thymeleaf] 유틸리티 객체와 날짜 - 				
    
    	유틸리티 객체와 날짜

 

'BackEnd > Thymeleaf' 카테고리의 다른 글

[Thymeleaf] Literals  (0) 2022.01.26
[Thymeleaf] URL 링크  (0) 2022.01.26
[Thymeleaf] 기본 객체들  (0) 2022.01.26
[Thymeleaf] SpringEL, 지역변수  (0) 2022.01.26
[Thymeleaf] 텍스트 - text, utext  (0) 2022.01.26

댓글