将@RequestParam用于多个POST参数 [英] Using @RequestParam for multiple POST-Parameters

查看:5090
本文介绍了将@RequestParam用于多个POST参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在此线程中解决了一个非常类似的问题: 点击



为了简短说明一下:



我用jQuery动态生成表单元素结果是这样的:

 < div id =XYZ-1> 
< input type =textid =XYZ-1-positionname =XYZ-1-positionstyle =width:20px;/>
< input type =textid =XYZ-1-inputname =XYZ-1-valuestyle =width:400px;/>
< / div>

< div id =XYZ-2>
< input type =textid =XYZ-2-positionname =XYZ-2-positionstyle =width:20px;/>
< input type =textid =XYZ-2-inputname =XYZ-2-valuestyle =width:400px;/>
< / div>

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

  @RequestMapping(value =/ data,method = RequestMethod.POST)
public String myHandler(@RequestParam(XYZ- 1-value)字符串内容,模型模型){

model.addAttribute(dataset,content);
返回数据

在我的帖子开头的线程中解决了:

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

这对于具有相同名称的输入类型有效: / p>

 < input type =checkboxname =myParam []value =myVal1/> 
< input type =checkboxname =myParam []value =myVal2/>

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



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



提前感谢

解决方案

我喜欢这种事情的模式是拥有一个包含列出(或地图)包装我的单个值的bean。所以,我会有:

  public class FormBean {
private List< FormItem>物品
//...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 =textname =formBean.items [0] .val1/> 
< input type =textname =formBean.items [0] .val2/>

或者您可以使用JSTL标签:

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

这样,一切都很好,打包起来,而且在查看方面很容易解析添加新行时的名称。



编辑



自动放置简单示例模型进入会话(我从内存中输入...请验证您自己)



控制器:



<$
@RequestMapping(/ form)
@SessionAttributes({formBean})
public class FormController {
@ RequestMapping()
public String defaultView(@ModelAttribute(formBean)FormBean formBean,Model uiModel){
//执行某些操作...添加任何要在表单中使用的uiModel。 ..你不应该需要添加formBean,因为它应该自动地使用@SessionAttributes注释
//自动调用会话//另外要注意,如果在Model或ModelAndView上设置formBean属性,那么将替换Session中的一个。此外,您可以将SessionStatus添加到其中一个方法,并使用.setComplete()方法来指示您已完成会话...您通常在知道用户完成应用程序后执行此操作,以便会话可以获取清理干净
}

@RequestMapping(/ someOtherPath)
public String someOtherHandler(@ModelAttribute(formBean)FormBean formBean,Model uiModel){
// Do再次,formBean应该被创建或不在会话中
}

@ModelAttribute(formBean)
private FormBean createFormBean(){
/ /这个方法可以命名任何东西,只要它返回一个FormBean,它的名称为@ModelAttribute,并且没有参数。使用此方法在创建FormBean时设置(设置默认值等)。
返回新的FormBean();
}
}


I have a very similiar problem solved in this thread: click

To make a long story short:

I'm generating form elements dynamically with jQuery and in the result it looks like this:

<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>

So for handling one form field, I'm using just this simple @ReqeustParam:

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

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

In the Thread of the beginning of my post it is solved like:

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

And this works for input types with the same name:

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

But the difference to my problem is, that my form elements do NOT have the same name - each has an individual Name.

So my question is how I can handle these individual post-parameters in a single handler in my controller.

Thanks in advance

解决方案

The pattern I like for this sort of thing is to have a form bean that contains a list (or map) of beans that wrap my individual values. So, I would have:

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

and

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

And controller method:

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

On the view-side, the mapping name would look like:

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

Or you could use the JSTL tags:

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

This way everything is nice and packaged up, and its really easy on the view-side to parse the name when adding new rows.

EDIT

Simple example on auto-placing models into session (I am typing this from memory...please validate on your own)

Controller:

@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();
    }
}

这篇关于将@RequestParam用于多个POST参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆