3

我在一个 jsp 页面上有两个表单。第一种形式不使用modelAttribute,而第二种形式使用modelAttribute。问题是,如果我发布第一个不使用 modelAttribute 的表单,则会声明我没有绑定 modelAttribute 的错误。

我已经在互联网上搜索以寻找解决方案,但我找不到有用的解决方案。

更改地址.jsp

<form method="post">
     <input type="hidden" name="id" value="0" />
     <input type="submit" name="exist" value="send to this address" />
</form>
<form:form method="post" modelAttribute="addressForm">
     <form:input path="street" />     
     <input type="submit" name="add" value="send to this address" />  
</form:form>

订单控制器.java

@RequestMapping(value="changeAddress",method = RequestMethod.GET)
public ModelAndView showChangAddress(Model model)
{
     model.addAttribute("addressForm", new AddressForm());
     return new ModelAndView("body.changeaddress");
}

@RequestMapping(value="changeAddress", params="add", method = RequestMethod.POST)
public ModelAndView addChangAddress(@ModelAttribute("addressForm") @Valid AddressForm af, BindingResult result, Model model)
{
     System.out.println("a");
     return new ModelAndView("body.changeaddress");
}

@RequestMapping(value="changeAddress", params="exist", method = RequestMethod.POST)
public ModelAndView processChangAddress(@RequestParam(value="id") String id, Model model)
{
     System.out.println("b");
     return new ModelAndView("body.changeaddress");
}

非常感谢帮助:)

4

1 回答 1

3

关于标签的spring form taglib文档:<form>

此标记呈现 HTML 'form' 标记并公开绑定路径到内部标记以进行绑定。它将命令对象放在 PageContext 中,以便内部标签可以访问命令对象。

我认为您不需要<form>第一种形式的弹簧标签中的任何东西。因此,您可以改用简单的 html 表单:

<form method="post" action="...">
     <input type="hidden" name="id" value="0" />
     <input type="submit" name="exist" value="send to this address" />
<form>
于 2013-01-03T11:01:35.317 回答