当我想要转到 myweb/register.html 时出现该错误有人可以给我建议我做错了什么吗?在 register.html 中,我突出显示了 ${User}、*{firstName}、*{lastName}、*{email} 和 *{password}路径 [] 上下文中 servlet [dispatcherServlet] 的 Servlet.service() 抛出异常 [请求处理失败;嵌套异常是org.thymeleaf.exceptions.TemplateInputException:模板解析期间发生错误(模板:“类路径资源[templates/register.html]”)],其根本原因java.lang.IllegalStateException:BindingResult 和 bean 名称“User”的普通目标对象都不能作为请求属性控制器: @RequestMapping(value = { "/register" }, method = RequestMethod.GET)public ModelAndView register(){ ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("register"); // resources/templates/register.html return modelAndView;注册.html:<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"><head> <title>Registration Form</title> <!-- link rel="stylesheet" type="text/css" th:href="@{/css/registration.css}" /--> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script></head><body style="background-color: #ededed;"><div style="background-color: #337ab7; height: 50px;"></div><div class="container-fluid" style="margin-top: 30px;"> <div class="row col-lg-4 col-lg-offset-4" style="margin-top: 40px; background-color: #fff; padding: 20px; border: solid 1px #ddd;"> <form autocomplete="off" action="#" th:action="@{/register}" th:object="${User}" method="POST" class="form-signin" role="form"> <h3 class="form-signin-heading">Registration Form</h3> <div class="form-group"> <div class=""> <input type="text" th:field="*{firstName}" placeholder="Name" class="form-control" /> </div>
1 回答
慕标琳琳
TA贡献1830条经验 获得超9个赞
您必须将空的 User 对象添加到模型中。
尝试以下操作:
@RequestMapping(value = { "/register" }, method = RequestMethod.GET)
public ModelAndView register(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("User", new User());
modelAndView.setViewName("register"); // resources/templates/register.html
return modelAndView;
添加回答
举报
0/150
提交
取消