0

我正在研究弹簧样本。我有一个名为 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 的正确解释。

4

1 回答 1

0

使用 modelAttribute 时,让用户:
1. 您的方法参数user被注释@ModelAttribute("user")
2.BindingResult声明类型的方法参数。
此外,注解参数值区分大小写,并且应该与您在 userForm.jsp 中声明的modelAttribute的值完全匹配。

于 2013-04-11T03:15:20.657 回答