【发布时间】:2021-03-29 16:59:50
【问题描述】:
我对百里香叶有疑问。在 html 文件中找不到 newUser 模型。
<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>
来自这个班级:
package Application.Controllers;
@Controller
@RequestMapping("/")
public class TemplateController {
private final RoleService roleService;
@Autowired
public TemplateController(RoleService roleService) {
this.roleService = roleService;
}
@GetMapping("register")
public String registerUser(Model model){
model.addAttribute("roles", roleService.listRoles());
model.addAttribute("newUser", new Users());
return "redirect:login";
}
}
问题是从其他类和 html 可以正常工作。
你能告诉我有什么问题吗?
【问题讨论】:
-
模型属性是请求属性,但您正在执行重定向。使用会话/闪存属性,或更改您的
/login处理程序以自行添加这两个属性。
标签: java spring spring-boot spring-mvc thymeleaf