HTML에서 <, > 는 태그로 인식되기 때문에 Thymeleaf에서는 텍스트 별칭을 사용하는 것을 권장
기호
텍스트 별칭
의미
>
gt
크다
<
lt
작다
>=
ge
크거나 같다
<=
le
작거나 같다
==
eq
같다
!=
ne
같지 않다
<!-- 기호 사용 (HTML에서 < > 가 태그로 해석될 수 있어 주의) -->
<p th:if="${user.age >= 20}">성인입니다.</p>
<!-- 텍스트 별칭 사용 (권장) -->
<p th:if="${user.age ge 20}">성인입니다.</p>
<p th:if="${user.age lt 20}">미성년자입니다.</p>
<p th:if="${user.role eq 'ADMIN'}">관리자입니다.</p>
5. 에러 처리
1) th:errors
Spring Validation과 연동해서 폼 유효성 검사 실패 시 필드별 에러 메시지를 출력하는 속성
BindingResult에 담긴 에러를 뷰에서 출력할 때 사용
해당 필드에 에러가 없으면 태그 자체가 렌더링되지 않음
<form th:action="@{/user/save}" method="post" th:object="${user}">
<input type="text" th:field="*{name}" />
<!-- name 필드에 에러가 있을 때만 출력 -->
<span th:errors="*{name}"></span>
<input type="email" th:field="*{email}" />
<span th:errors="*{email}"></span>
<button type="submit">저장</button>
</form>
// Controller
@PostMapping("/user/save")
public String save(@Valid UserDto user, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
return "user/form"; // 에러가 있으면 폼으로 다시 이동
}
userService.save(user);
return "redirect:/user/list";
}