首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对多个后置参数使用@RequestParam

对多个后置参数使用@RequestParam
EN

Stack Overflow用户
提问于 2013-02-01 19:52:09
回答 2查看 6.3K关注 0票数 0

在这个线程中,我有一个非常相似的问题:https://stackoverflow.com/questions/4596351/binding-a-list-in-requestparam

长话短说:

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

代码语言:javascript
复制
<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:

代码语言:javascript
复制
     @RequestMapping(value = "/data", method = RequestMethod.POST)
 public String myHandler(@RequestParam("XYZ-1-value")String content, Model model) {

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

在我的帖子开头的线程中,它的解决方式如下:

代码语言:javascript
复制
public String controllerMethod(@RequestParam(value="myParam[]") String[] myParams){
....
}

这适用于同名的输入类型:

代码语言:javascript
复制
<input type="checkbox" name="myParam[]" value="myVal1" />
<input type="checkbox" name="myParam[]" value="myVal2" />

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

因此,我的问题是如何在控制器中的单个处理程序中处理这些单独的post参数。

提前感谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-02-01 20:08:39

对于这种情况,我喜欢的模式是有一个表单bean,它包含一个bean列表(或映射)来包装我的单个值。所以,我会:

代码语言:javascript
复制
public class FormBean {
  private List<FormItem> items;
  //...getters/setters
}

代码语言:javascript
复制
public class FormItem {
  private String val1;
  private Integer val2;
  //...getters/setters
}

和控制器方法:

代码语言:javascript
复制
@RequestMapping()
public String default(@ModelAttribute("formBean") FormBean formBean) {
   // Blah blah blah
}

在视图端,映射名如下所示:

代码语言:javascript
复制
<input type="text" name="formBean.items[0].val1" />
<input type="text" name="formBean.items[0].val2" />

或者您可以使用JSTL标记:

代码语言:javascript
复制
<form:form modelAttribute="formBean">
  <form:input path="items[0].val1" />
  <form:input path="items[0].val2" />
</form:form>

通过这种方式,一切都很好,而且打包起来很简单,在添加新行时,视图端可以很容易地解析名称。

编辑

关于将模型自动放置到会话中的简单示例(我正在通过memory...please验证自己输入此示例)

主计长:

代码语言:javascript
复制
@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
EN

Stack Overflow用户

发布于 2013-02-01 20:08:47

首先,您不需要[] --没有它它就能工作。但是,如果每个复选框的名称不同,那么每个复选框都需要一个单独的@RequestParam

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14653940

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档