下面是我的JSP和控制器类,而我在tomcat中运行我的应用程序时遇到此异常。
<form:form action="userDetails" method="post" commandName="user">
<form:label path="firstName">First Name</form:label>
<form:input path="firstName"/><br/>
<form:label path="lastName">Last Name</form:label>
<form:input path="lastName"/><br/>
<form:label path="nickName">Nick Name</form:label>
<form:input path="nickName"/><br/>
<form:label path="emailAddress">Email Address</form:label>
<form:input path="emailAddress"/><br/>
<input type="submit" value="submit"/>
</form:form>
控制器:
@RequestMapping(value="userDetails",method=RequestMethod.GET)
public String getEmployeeDetailsPost(@ModelAttribute("user")User user,ModelMap map){
map.put("firstName", user.getFirstName());
map.put("lastName", user.getLastName());
map.put("nicName", user.getNickName());
map.put("email", user.getEmailAddress());
return "success";
}
答案 0 :(得分:0)
只需将此方法添加到您的控制器:
@ModelAttribute("user")
public User getUserForm() {
return new User();
}
您必须使其可用于请求。通过这种方法,spring将在模型中初始化User
对象,并且能够在表单提交请求上分配值。
答案 1 :(得分:0)
尝试这样的事情:
控制器:
@RequestMapping(value = "/userDetails", method = RequestMethod.GET)
public String userFormRender(Model model)
{
model.addAttribute("user", new User());
return "jspPageName";
}
@RequestMapping(value = "/userDetails", method = RequestMethod.POST)
public String userFormAction(@ModelAttribute("user") User user, Model model)
{
System.out.println("User firstName: " + user.getFirstName());
return "redirect:/";
}
JSP:
<form:form method="post" commandName="user">
<form:label path="firstName">First Name</form:label>
<form:input path="firstName"/><br/>
<form:label path="lastName">Last Name</form:label>
<form:input path="lastName"/><br/>
<form:label path="nickName">Nick Name</form:label>
<form:input path="nickName"/><br/>
<form:label path="emailAddress">Email Address</form:label>
<form:input path="emailAddress"/><br/>
<input type="submit" value="submit"/>
</form:form>