我正在研究弹簧样本。我有一个名为 UserController2 的控制器类,如下所示
@Controller
public class UserController2 extends MultiActionController {
private UserDAO userDAO;
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
@RequestMapping(params = "add", method = RequestMethod.POST)
public ModelAndView add(HttpServletRequest request,
HttpServletResponse response, User user) throws Exception {
userDAO.saveUser(user);
return new ModelAndView("redirect:User.htm");
}
public ModelAndView list(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelMap modelMap = new ModelMap();
modelMap.addAttribute("userList", userDAO.listUser());
modelMap.addAttribute("User", new User());
return new ModelAndView("userForm", modelMap);
}
}
我有一个名为 userForm.jsp 的 jsp 页面
<form:form method="POST" action="add.htm" commandName="User" modelAttribute="User">
<table>
<tr>
<td><form:label path="name">Name</form:label></td>
<td><form:input path="name" /></td>
</tr>
<tr>
<td><form:label path="id">id</form:label></td>
<td><form:input path="id" /></td>
</tr>
<tr>
<td><form:label path="password">Name</form:label></td>
<td><form:input path="password" /></td>
</tr>
<tr>
<td><form:label path="gender">Name</form:label></td>
<td><form:input path="gender" /></td>
</tr>
<tr>
<td><form:label path="gender">Name</form:label></td>
<td><form:input path="gender" /></td>
</tr>
<tr>
<td><form:label path="country">Name</form:label></td>
<td><form:input path="country" /></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Submit"/>
</td>
</tr>
</table>
</form:form>
以下代码实际上返回了什么
return new ModelAndView("userForm", modelMap);
和
return new ModelAndView("redirect:User.htm");
我收到以下错误
java.lang.IllegalStateException: Neither BindingResult nor plain target object for
bean name 'User' available as request attribute
我进行了搜索,但找不到对 modelandview 的正确解释。