當(dāng)我想要轉(zhuǎn)到 myweb/register.html 時(shí)出現(xiàn)該錯(cuò)誤有人可以給我建議我做錯(cuò)了什么嗎?在 register.html 中,我突出顯示了 ${User}、*{firstName}、*{lastName}、*{email} 和 *{password}路徑 [] 上下文中 servlet [dispatcherServlet] 的 Servlet.service() 拋出異常 [請(qǐng)求處理失??;嵌套異常是org.thymeleaf.exceptions.TemplateInputException:模板解析期間發(fā)生錯(cuò)誤(模板:“類路徑資源[templates/register.html]”)],其根本原因java.lang.IllegalStateException:BindingResult 和 bean 名稱“User”的普通目標(biāo)對(duì)象都不能作為請(qǐng)求屬性控制器: @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 回答

慕標(biāo)琳琳
TA貢獻(xiàn)1830條經(jīng)驗(yàn) 獲得超9個(gè)贊
您必須將空的 User 對(duì)象添加到模型中。
嘗試以下操作:
@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;
添加回答
舉報(bào)
0/150
提交
取消