问题描述
我有用于处理表单提交的控制器方法:
I have the controller method for processing form submitting:
@RequestMapping(method = {RequestMethod.POST})
public String submitForm(...){...}
但是,我有一个新的测试用例:
But, I have got a new test case:
如果Form具有参数ProductData
,则调用控制器方法submitFormWithProductData
.我为此遇到了困难,因为ProductData
是Map
.表单标签中站点ProductData
"字段上的内容如下:
If Form has parameter ProductData
call controller method submitFormWithProductData
. and I am facing difficulties with this, because ProductData
is a Map
. On Site ProductData
field in form tag looks like:
<input type="text" name="productData['param1']">
<input type="text" name="productData['param2']">
我不知道如何为submitFormWithProductData
方法创建正确的@RequestMapping
注释.
And I don't know, how to create right @RequestMapping
annotation for submitFormWithProductData
method.
我尝试过:
@RequestMapping(method = {RequestMethod.POST}, params="productData")
和
@RequestMapping(method = {RequestMethod.POST}, params="productData[]")
但是我没有成功.
推荐答案
productData
必须是模型对象的属性.
productData
has to be a property of a model object.
public class FormModel {
private Map<String,String> productData = ...;
...
}
为此,您必须创建一个类似的处理程序方法:
according to this you have to create a handler method like that:
@RequestMapping(....)
public String submitFormWithProductData(FormModel formModel) {
....
}
Spring会自动将productData参数绑定到FormModel
对象中的相应属性.
Spring will automatically bind the productData parameters to the according property in the FormModel
object.
但我不知道您为什么要以不同的方式处理它.您可以添加一个隐藏的输入字段productDataSubmitted
并添加以下处理程序:
But I don't know why you want to handle it differently. You could add a hidden input field productDataSubmitted
and add the following handler:
@RequestMapping(method = {RequestMethod.POST}, params="productDataSubmitted")
这篇关于Spring MVC:@RequestMapping:如何捕获Map参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!