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>

したがって、1 つのフォーム フィールドを処理するために、次の単純な @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

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

First, you don't need the [] - it will work without it. But if the name of each checkbox differs, then you need a separate @RequestParam for each checkbox.

于 2013-02-01T20:08:47.623 に答える