java 一个jsp中的多个表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14137281/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 15:11:57  来源:igfitidea点击:

Multiple forms in one jsp

javaformsspringjspmultiple-forms

提问by Skyverian

I have two form on one jsp page. The first form doesn't use the modelAttribute and the second one uses an modelAttribute. What the problem is, is that if I post the first form which I doesn't use the modelAttribute will claim an error that I haven't bind the modelAttribute.

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

I have searched on the internet to look for solutions but I can't find one which was helpfull.

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

changeAddress.jsp

更改地址.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>

OrderController.java

订单控制器.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");
}

Much appriciated for help :)

非常感谢帮助:)

回答by micha

The spring form taglib documentation about the <form>tag:

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

This tag renders an HTML 'form' tag and exposes a binding path to inner tags for binding. It puts the command object in the PageContext so that the command object can be accessed by inner tags.

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

I think you don't need anything from the spring <form>tag in your first form. So you can use a simple html form instead:

我认为您不需要<form>第一种形式的 spring标签中的任何内容。所以你可以使用一个简单的 html 表单来代替:

<form method="post" action="...">
     <input type="hidden" name="id" value="0" />
     <input type="submit" name="exist" value="send to this address" />
<form>