728x90
반응형
토이프로젝트를 진행하던 중 get방식으로 글작성하는 페이지를 랜더링 하던 중 에러가 발생했다. 여러가지 에러중에 타임리프 경로에 관한 에러도 있었기 때문에 경로를 올바르게 변경해줘도 오류가 떠서 구글링을 해봤다.
현재 내 Posting Form의 html 코드와 PostController코드이다.
<div class="layout">
<!--action: 해당form 안의 데이터를어디로보내줄거냐-->
<form action="/freeboard/posting" method="post" th:object="${postDTO}">
<input th:field="*{title}" type="text" placeholder="제목"> <!--네임설정-->
<textarea th:field="*{content}" type="text" placeholder="본문"></textarea>
<button type="submit">작성</button>
<!--버튼을눌렀을때(submit) form안에있는 데이터가 action에서 지정한 주소로 넘어간다-->
</form>
</div>
controller의 /freeboard/posting 경로로 들어오는 post방식의 값을 postDTO로 전송하고있다.
@GetMapping("/posting")
public String getBoardWrite() {
return "board/postwrite";
}
나는 단지 페이지를 랜더링하기 때문에 아무거도 안보내줘도 되고 해당 form에 대한 html코드가 있는 경로만 잘 매칭시켜주면 된다고 생각했다. 구글링 결과 내가 랜더링 하는 페이지는 form을 랜더링 하는데 템플릿에 th:object="${postDTO}"에서 postDTO에 대한 정보를 전달받지 못해서 발생한 것이다. 그렇기 때문에 폼을 랜더링 할 때 빈 postDTO객체를 전달해주면 정상적으로 실행된다.
@GetMapping("/posting")
public String getBoardWrite(Model model) {
model.addAttribute("postDTO", new PostDTO());
return "board/postwrite";
}
728x90
반응형