[점프투스프링부트] 2-15 질문 등록과 폼 질문 드립니다.
안녕하세요. 다시 질문이 있어서 글을 올립니다. 오류나는 부분만 글을 올립니다.
오류 내용은 An error happened during template parsing (template: "class path resource [templates/daywork_detail.html]")
Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring6.processor.SpringTextareaFieldTagProcessor' (template: "daywork_detail" - line 33, col 19)
Neither BindingResult nor plain target object for bean name 'dayworkanswerForm' available as request attribute
at org.springframework.web.servlet.support.BindStatus.
<!-- 답변 반복 끝 -->
<!-- 답변 작성 -->
<form th:action="@{|/dayworkanswer/create/${daywork.id}|}" th:object="${dayworkanswerForm}" method="post" class="my-3">
<div class="alert alert-danger" role="alert" th:if="${#fields.hasAnyErrors()}">
<div th:each="err : ${#fields.allErrors()}" th:text="${err}" />
</div>
<textarea th:field="*{content}" rows="10" class="form-control"></textarea>
<input type="submit" value="답변등록" class="btn btn-primary my-2">
</form>
</div>
</html>
import jakarta.validation.constraints.NotEmpty;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class DayWorkAnswerForm {
@NotEmpty(message = "내용은 필수항목입니다.")
private String content;
}
@RequestMapping("/dayworkanswer")
@RequiredArgsConstructor
@Controller
public class DayWorkAnswerController {
private final DayWorkService dayworkService;
private final DayWorkAnswerService dayworkanswerService;
@PostMapping("/create/{id}")
public String createAnswer(Model model, @PathVariable("id") Integer id, @Valid DayWorkAnswerForm dayworkanswerForm, BindingResult bindingResult) {
DayWork daywork = this.dayworkService.getDayWork(id);
// TODO: 답변을 저장한다.
if (bindingResult.hasErrors()) {
model.addAttribute("daywork", daywork);
return "daywork_detail";
}
this.dayworkanswerService.create(daywork, dayworkanswerForm.getContent());
return String.format("redirect:/daywork/detail/%s", id);
}
}
kimo87 님 345
2023년 8월 11일 3:24 오후