'Thymeleaf cannot resolve
I have this Controller Class and an HTML Login page. Thymeleaf Cannot resolve 'loginRequest'.
@Controller
public class ProfessorController {
private final ProfessorService professorService
@Autowired
public ProfessorController(ProfessorService professorService){
this.professorService = professorService;
}
@GetMapping("/login")
public String getLoginPage(Model model){
model.addAttribute("loginRequest", new Professor());
return "login_page";
}
@PostMapping("/login")
public String login(@ModelAttribute Professor professor, Model model){
System.out.println("login request: " + professor);
Professor authenticated =
professorService.authenticate(professor.getUsername(),professor.getPassword());
if(authenticated != null){
model.addAttribute("userLogin",authenticated.getUsername());
return "home";
}else{
return "error_page";
}
}
}
I have this in my html code: xmlns:th="http://www.thymeleaf.org"
<form class="login100-form validate-form" method="post" action="/login" th:object="${loginRequest}">
Solution 1:[1]
I had the same problem actually.. I had a project that was working fine, resolving Thymeleaf template variables.. so I compared the two projects..the only difference I could fine was the SpringBoot version in the POM. I was using 2.7 in my new project.. I reverted to 2.5.2 and it was able to resolve the variables. Hope this helps. Note: 2.6.8 appears to work fine as well.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
Solution 2:[2]
I think I know the mistake, please try the below code in Thymeleaf template.
<form class="login100-form validate-form" method="post" th:action="@{/login}" th:object="${loginRequest} id="login_page"">
I think the issue is that you are missing the id property. Also for the action property the th: part is missing. it should be th:action and also modified the value @{/login}.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 |
