Spring/스프링부트

org.thymeleaf.exceptions.TemplateInputException 에러 해결방법

Everybody's Service 2021. 7. 14. 15:05

Spring Security를 구현하던 도중 Thymeleaf관련 에러가 계속 터져서 해결법을 찾으나 도저히 나오지 않았다.

 

 

결론만 짧게 말하자면,

 

1. Controller 코드

 

@GetMapping("/signup")
public String signup(Model model){
MemberTo memberTo = new MemberTo();
model.addAttribute("memberTo", memberTo);
return "signup";
}


@PostMapping("/signup")
public String signup(@ModelAttribute MemberTo memberTo){
service.save(memberTo);

return "redirect:/login";
}

 

2. thymeleaf 코드

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" content="text/html">
<title>Title</title>
</head>
<body>
<h1>Sign up</h1>

<form action="#" th:action="@{/signup}" th:object="${memberTo}" method="post">
<input type="text" th:field="*{id}">
<input type="password" th:field="*{pw}">
<input type="text" th:field="*{first_name}">
<input type="text" th:field="*{last_name}">
<input type="text" th:field="*{email}">
<input type="submit" value="submit">
</form>
</body>
</html>

 

Controller에서 signup경로로 들어가면 thymeleaf에서 바인딩을 하는 예제이다.

그리고 바인딩할 객체를

 

ax.persistenc
@Getter
@Setter
public class MemberTo {


private String id;

private String pw;

private String first_name;

private String last_name;

private String email;

public Member toEntity() {

return new Member(id,pw,first_name,last_name, email);
}

}

 

위와 같이 구현해놓았다고 할때, Thymeleaf에서 th:field로 입력되는 값은

백엔드단에서 객체의 setter를 이용해 Thymeleaf가 알아서 객체에 값을 주입해준다.

 

나같은 경우, @Setter 애노테이션을 추가하지 않아 Thymeleaf가 객체에 값을 주입하지 못해서

TemplateInputException 에러가 계속 터졌다.

 

따라서 객체에 Setter를 까먹지 않았는지 확인한다.

 

 

 

 

 

 

 

 

코드 구현은 스프링 공식 웹사이트 참고

 

https://spring.io/guides/gs/handling-form-submission/

 

Handling Form Submission

this guide is designed to get you productive as quickly as possible and using the latest Spring project releases and techniques as recommended by the Spring team

spring.io