0

我在这个线程中解决了一个非常相似的问题:单击

使长话短说:

我正在使用 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" />

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

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

提前致谢

4

2 回答 2

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();
    }
}
于 2013-02-01T20:08:39.753 回答
0

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

于 2013-02-01T20:08:47.623 回答