@Vaild 어노테이션으로 유효성 검사를 하기 위해 다음과 같은 HTML 코드를 작성했다.
<form th:action="@{/articles/post}" method="post" th:object="${communityBoardDto}" enctype="multipart/form-data">
<input type="submit" class="write-button">
<input type="text" th:field="*{title}" placeholder="제목을 입력해주세요">
<p th:if="${#fields.hasErrors('title')}" th:errors="*{title}" class="fieldError">Incorrect data</p>
<hr/>
<textarea th:field="*{content}" placeholder="내용을 입력해주세요"></textarea>
<p th:if="${#fields.hasErrors('content')}" th:errors="*{content}" class="fieldError">Incorrect data</p>
<hr/>
</form>
그리고 해당 뷰를 렌더링 하기 위해 다음과 같은 컨트롤러를 작성했다.
@GetMapping("/articles/post")
public String showArticleForm(Model model) {
return "contents/community/community-write";
}
하지만 렌터링이 되지 않는 에러가 발생했고, th:object 를 사용하려면 dto를 model에 담아줘야 한다는 사실을 알게됐다.
@GetMapping("/articles/post")
public String showArticleForm(Model model) {
model.addAttribute("communityBoardDto", new CommunityBoardDto());
return "contents/community/community-write";
}
여러모로 손이 많이가는 타임리프
'코딩딩 > Spring' 카테고리의 다른 글
Mockito를 활용한 단위 테스트 정리 (3) | 2024.12.11 |
---|---|
JUnit5 정리 (1) | 2024.12.11 |
Entity의 Id값을 Long으로 주는 이유가 뭘까? (0) | 2024.01.02 |
JPA 엔티티 컬렉션 성능 최적화 (0) | 2023.12.30 |
JPQL (0) | 2023.11.18 |