java - 一个jsp中的多个表单

标签 java forms spring jsp multiple-forms

我在一个 jsp 页面上有两个表单。第一种形式不使用 modelAttribute,第二种形式使用 modelAttribute。问题是,如果我发布第一个不使用 modelAttribute 的表单,将会报错说我没有绑定(bind) 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>

OrderController.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");
}

非常感谢您的帮助:)

最佳答案

spring 表单标签库 documentation 关于 <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.

我想你不需要 Spring 的任何东西<form>在您的第一种形式中标记。因此,您可以改用简单的 html 表单:

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

关于java - 一个jsp中的多个表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14137281/

相关文章:

java - java中 'new'的语法

django - 什么时候应该使用 Form/ModelForm、FormSet/ModelFormSet?

javascript - 条件 ng-required 集体检查 3 个输入框不起作用

java - 奇怪的错误 : Beans not created, "Can not configure endpoints"

java - 如何让 Spring Boot 基于外部配置配置 RabbitMQ 配置?

java - 使用 `new EdgeOptions()` 优于 `DesiredCapabilities.edge()` 通过 Jenkins 和 Selenium 启动远程 Microsoft Edge 浏览器错误

java - 如何在 android 中设置单个 listView 项目的样式?

java - 操作栏选项操作

javascript - 当用户刷新网页时阻止表单提交

spring - 如何使用自定义数据源动态连接存储库?