在这个线程中,我有一个非常相似的问题:https://stackoverflow.com/questions/4596351/binding-a-list-in-requestparam
长话短说:
我使用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" />但是,与我的问题不同的是,我的表单元素没有相同的名称--每个元素都有一个单独的名称。
因此,我的问题是如何在控制器中的单个处理程序中处理这些单独的post参数。
提前感谢
发布于 2013-02-01 20:08:39
对于这种情况,我喜欢的模式是有一个表单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>通过这种方式,一切都很好,而且打包起来很简单,在添加新行时,视图端可以很容易地解析名称。
编辑
关于将模型自动放置到会话中的简单示例(我正在通过memory...please验证自己输入此示例)
主计长:
@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-01 20:08:47
首先,您不需要[] --没有它它就能工作。但是,如果每个复选框的名称不同,那么每个复选框都需要一个单独的@RequestParam。
https://stackoverflow.com/questions/14653940
复制相似问题