使用@RequestParam进行多个POST参数

时间:2013-02-01 19:52:10

标签: java spring data-binding spring-mvc

我在这个帖子中遇到了一个非常类似的问题:click

简而言之:

我正在使用jQuery动态生成表单元素,结果如下所示:

<div id="XYZ-1">
<input type="text" id="XYZ-1-position" name="XYZ-1-position" style="width: 20px;"/>
<input type="text" id="XYZ-1-input" name="XYZ-1-value" style="width: 400px;"/>
</div>

<div id="XYZ-2">
<input type="text" id="XYZ-2-position" name="XYZ-2-position" style="width: 20px;"/>
<input type="text" id="XYZ-2-input" name="XYZ-2-value" style="width: 400px;"/>
</div>

因此,为了处理一个表单字段,我只使用这个简单的@ReqeustParam:

     @RequestMapping(value = "/data", method = RequestMethod.POST)
 public String myHandler(@RequestParam("XYZ-1-value")String content, Model model) {

    model.addAttribute("dataset", content); 
            return "data"

在帖子开头的帖子中解析如下:

public String controllerMethod(@RequestParam(value="myParam[]") String[] myParams){
....
}

这适用于具有相同名称的输入类型:

<input type="checkbox" name="myParam[]" value="myVal1" />
<input type="checkbox" name="myParam[]" value="myVal2" />

但我的问题的不同之处在于,我的表单元素没有相同的名称 - 每个都有一个单独的名称。

所以我的问题是如何在我的控制器中的单个处理程序中处理这些单独的后置参数。

提前致谢

2 个答案:

答案 0 :(得分:1)

我喜欢这种模式的模式是让一个表单bean包含一个包含我个人值的bean的列表(或映射)。所以,我会:

public class FormBean {
  private List<FormItem> items;
  //...getters/setters
}

public class FormItem {
  private String val1;
  private Integer val2;
  //...getters/setters
}

和控制器方法:

@RequestMapping()
public String default(@ModelAttribute("formBean") FormBean formBean) {
   // Blah blah blah
}

在视图方面,映射名称如下所示:

<input type="text" name="formBean.items[0].val1" />
<input type="text" name="formBean.items[0].val2" />

或者你可以使用JSTL标签:

<form:form modelAttribute="formBean">
  <form:input path="items[0].val1" />
  <form:input path="items[0].val2" />
</form:form>

这样一切都很好并且打包了,在添加新行时,在视图端很容易解析名称。

修改

将模型自动放入会话的简单示例(我从内存中输入此内容...请自行验证)

控制器:

@Controller
@RequestMapping("/form")
@SessionAttributes({"formBean"})
public class FormController {
    @RequestMapping()
    public String defaultView(@ModelAttribute("formBean") FormBean formBean, Model uiModel) {
        // Do something...add anything to the uiModel that you want to use in your form...you should not need to add formBean as it should auto-matically get put into session now with the @SessionAttributes annotation
        // Also to note, if you set the "formBean" attribute on the Model or ModelAndView, it will replace the one in Session.  Also, you can add the SessionStatus to one of your methods and use its .setComplete() method to indicate you are done with the session...you usually do this after you know the user is done with the application so the session can get cleaned up.
    }

    @RequestMapping("/someOtherPath")
    public String someOtherHandler(@ModelAttribute("formBean") FormBean formBean, Model uiModel) {
        // Do something...again, formBean should be either created or out of session
    }

    @ModelAttribute("formBean")
    private FormBean createFormBean() {
        // This method can be named anything, as long as it returns a FormBean, has the @ModelAttribute named on it, and has no arguments.  Use this method to populate your FormBean when its created (set defaults and such).
        return new FormBean();
    }
}

答案 1 :(得分:0)

首先,您不需要[] - 没有它就可以使用它。但是,如果每个复选框的名称不同,则每个复选框都需要单独的@RequestParam