타임리프 HTML Model에 DTO(Form Class) ParseException -> 바인딩 명시적 추가로 해결

박철현·2023년 8월 2일
0

문제해결

목록 보기
9/17
  • 문제 : 점프투스프링부트 추가기능 - 비밀번호 변경 기능 구현 중, HTML에서 form 바인딩이 안되던 문제
    • 오류 메세지
      Caused by: org.attoparser.ParseException: Exception evaluating SpringEL expression: "#fields.hasAnyErrors()" (template: "common/form_errors" - line 2, col 19)
    • 문제 되던 부분
      pw_change.html
 <form th:action method="post" th:object="${pwChangeForm}" onsubmit="ChangePasswordForm__submit(this); return false;">
    <div th:replace="~{common/form_errors :: formErrorsFragment}"></div>

form_errors.html

<div th:fragment="formErrorsFragment" class="alert alert-danger"
     role="alert" th:if="${#fields.hasAnyErrors()}">
    <div th:each="err : ${#fields.allErrors()}" th:text="${err}"> </div>
</div>
  • 원인 : 원인 미상. 왜인지 모르겠음. 다른 것들 모두 이렇게 했는데 이것만 안됨..

  • 정말 모르겠네..

  • 해결 : 컨트롤러에서 @ModelAttribute를 사용하여 명시해줌
    위에 pw_change.html 일부 코드 보면 pwChangeForm로 매핑되라고 object 지정해줬고
    컨트롤러에서도 pwChangeForm 이렇게 해줬는데,.. 어이가 없음 이것만 이럼..

  • 기존 Controller

@PreAuthorize("isAuthenticated()")
	@GetMapping("/change/passwd")
	public String showChangePW(PWChangeForm pwChangeForm) {
		return "user/pw_change";
	}
  • 해결 Controlller
    @PreAuthorize("isAuthenticated()")
    	@GetMapping("/change/passwd")
    	public String showChangePW(@ModelAttribute("pwChangeForm") PWChangeForm pwChangeForm) {
    		return "user/pw_change";
    	}
  • 하.. 그래도 기능 구현 성공. GPT 말대로 안되면 어노테이션 붙이기 명심하기..
  • 출처 : 뤼튼(GPT 4.0)
profile
비슷한 어려움을 겪는 누군가에게 도움이 되길

1개의 댓글

comment-user-thumbnail
2023년 8월 2일

글 잘 봤습니다.

답글 달기