Java Thymeleaf找不到声明
我对百里香有问题。在html文件中找不到新用户模型Java Thymeleaf找不到声明,java,spring,spring-boot,spring-mvc,thymeleaf,Java,Spring,Spring Boot,Spring Mvc,Thymeleaf,我对百里香有问题。在html文件中找不到新用户模型 <form th:action="@{/users/add}" th:object="${newUser}" method="post"> <p>User Name: <input type="text" th:field="*{userName}"></p> <p>Em
<form th:action="@{/users/add}" th:object="${newUser}" method="post">
<p>User Name: <input type="text" th:field="*{userName}"></p>
<p>Email: <input type="text" th:field="*{email}"></p>
<p>Password: <input type="text" th:field="*{password}"></p>
<p>Role:
<select th:field="*{role}">
<option th:each="role: ${roles}" th:value="${role.getId()}" th:utext="${role.getUserRole()}">
</option>
</select>
</p>
<p><input type="submit" value="Add User"></p>
</form>
问题是,从其他类和html来看,它工作得很好
您能告诉我怎么了吗?在您的登录中,请确保添加以下行:
model.addAttribute("roles", roleService.listRoles());
model.addAttribute("newUser", new Users());
或者更改registerUser返回的只是视图不重定向到其他操作:
@GetMapping("register")
public String registerUser(Model model){
model.addAttribute("roles", roleService.listRoles());
model.addAttribute("newUser", new Users());
return "login";
}
模型属性是请求属性,但您正在执行重定向。使用session/flash属性,或者更改您的
/login
处理程序来添加这两个属性本身。谢谢,第二个解决方案使用了return“register”,但为什么我在重定向时必须在login方法中定义新用户和角色?一个返回视图,而后面的重定向到另一个控制器请求映射操作
@GetMapping("register")
public String registerUser(Model model){
model.addAttribute("roles", roleService.listRoles());
model.addAttribute("newUser", new Users());
return "login";
}