일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
- mysql설치하기
- mysql
- 타임리프URL
- mybatis
- 스프링부트설정
- 정처기실기요약
- mysql다운로드
- thymeleaf
- 이클립스없이cmd
- cmd에서java파일실행
- 타임리프Unescape
- 타임리프기본객체
- spring
- 개체관계모델
- 정보처리기사
- 타임리프날짜
- 정처기실기
- 타임리프 표현식
- MySQL설치순서
- 타임리프유틸리티객체
- ER모델
- 타임리프Escape
- HelloWorld출력
- git
- java
- 타임리프 특징
- 타임리프SpringEL
- 정보처리기사실기
- 정보처리기사실기요약
- 타임리프변수
- Today
- Total
ye._.veloper
[ SpringMVC2 ] Thymeleaf (타임리프 기본, 유틸리티 객체와 날짜, URL 링크) 본문
☁ 기본 객체들
◽ 타임리프는 기본 객체들을 제공
· ${#request}, ${#response}, ${#session}, ${#servletContext} : Spring Boot 3.0부터 제공 X
· ${#locale}
💡 #request는 HttpServletRequest 객체가 그대로 제공되기 때문에 데이터를 조회하려면
request.getParameter("data")처럼 불편하게 접근해야 한다.
✅ 위와 같은 점을 해결하기 위해 아래와 같이 편의 객체도 제공한다.
· HTTP 요청 파라미터 접근 : param
ex) ${param.paramData}
· HTTP 세션 접근 : session
ex) ${session.sessionData}
· Spring Bean 접근 : @
ex) ${@helloBean.hello('Spring!')}
@GetMapping("/basic-objects")
public String basicObjects(HttpSession session) {
session.setAttribute("sessionData", "Hello Session");
return "basic/basic-objects";
}
Spring Boot 3.0 이상인 경우
· 위와 같이 기본 객체를 불러올 경우, 에러가 발생한다.
버전이 3.0 이상이라면 아래와 같이 기본 객체를 불러올 수 있다.
@GetMapping("/basic-objects")
public String basicObjects(Model model, HttpServletRequest request, HttpServletResponse response, HttpSession session) {
session.setAttribute("sessionData", "Hello Session");
model.addAttribute("request", request);
model.addAttribute("response", response);
model.addAttribute("servletContext", request.getServletContext());
return "basic/basic-objects";
}
☁ 유틸리티 객체와 날짜
◽ 타임리프는 문자, 숫자, 날짜, URI 등을 편리하게 다루는 다양한 유틸리티 객체들을 제공한다.
객체 | 설명 |
#message | 메세지, 국제화 처리 |
#uris | URI 이스케이프 지원 |
#dates | java.util.Date 서식 지원 |
#calendars | java.util.Calendar 서식 지원 |
#temporals | Java 8 날짜 서식 지원 |
#numbers | 숫자 서식 지원 |
#strings | 문자 관련 편의 기능 |
#objects | 객체 관련 기능 제공 |
#bools | boolean 관련 기능 제공 |
#arrays | 배열 관련 기능 제공 |
#lists, #sets, #maps | Collection 관련 기능 제공 |
#ids | 아이디 처리 관련 기능 제공 |
Ref.
타임리프를 사용하다 유틸리티 객체를 사용할 때, 찾아서 사용하면 된다.
지금은 타임리프가 제공하는 유틸리티 객체에는 대략 이런 것들이 있다고만 알아두자.
☁ Java 8 날짜
· 타임리프에서 자바8 날짜인 LocalDate , LocalDateTime , Instant 를 사용하려면 추가 라이브러리가 필요하다.
스프링 부트 타임리프를 사용하면 해당 라이브러리가 자동으로 추가되고 통합된다.
🍃 타임리프 자바8 날짜 지원 라이브러리
· thymeleaf-extras-java8time
🌙 Java 8 날짜용 유틸리티 객체
· #temporals
/* Controller */
@GetMapping("/date")
public String date(Model model) {
model.addAttribute("localDateTime", LocalDateTime.now());
return "basic/date";
}
/* html 일부 */
<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>
☁ URL 링크
·타임리프에서 URL을 생성할 때, @{ ... } 문법을 사용
/* Controller */
@GetMapping("/link")
public String link(Model model) {
model.addAttribute("param1", "data1");
model.addAttribute("param2", "data2");
return "basic/link";
}
/* html 일부 */
<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>
✅ 단순 URL
@{/hello} ➡ /hello
✅ 쿼리 파라미터
@{ /hello ( param1 = ${param1}, param2 = ${param2} ) }
➡ /hello?param1=data1¶m2=data2
( ) 안에 있는 부분 ➡ Query Parameter로 처리됨
✅ 경로 변수
@{/hello/{param1}/{param2}(param1=${param1}, param2=${param2})}
➡ /hello/data1/data2
💡 URL 경로상에 변수가 있으면 () 부분은 경로 변수로 처리
✅ 경로 변수 + 쿼리 파라미터
@{/hello/{param1}(param1=${param1}, param2=${param2})}
➡ /hello/data1?param2=data2
💡 경로 변수와 쿼리 파라미터를 함께 사용할 수 있음
◽ 상대 경로, 절대 경로, 프로토콜 기준을 표현할 수도 있다.
· /hello : 절대 경로
· hello : 상대 경로
Ref. 스프링 MVC 2편 - 백엔드 웹 개발 활용 기술 (인프런 - 김영한님 강의)
'Spring' 카테고리의 다른 글
[ SpringBoot ] @SpringBootApplication / Bean 등록 (0) | 2023.07.09 |
---|---|
[ SpringMVC2 ] Thymeleaf (변수 - SpringEL) (0) | 2023.04.25 |
[ SpringMVC2 ] Thymeleaf 정의 | 기본 기능 (0) | 2023.04.23 |
[ IntelliJ ] Unmapped Spring configuration file : 파일명 (0) | 2023.02.14 |
[ Spring Boot ] 1. 게시판 만들기 - 초기 개발 설정하기 (0) | 2023.02.11 |