If a HTML form contains multiple input fields:
<form>
<input id="in1" type="text" value="one">
<input id="in2" type="text" value="two">
<input id="in3" type="text" value="three">
</form>
and is passed to a Spring controller as a serialized form like this:
new Ajax.Request('/doajax',
{asynchronous:true, evalScripts:true,
parameters: $('ajax_form').serialize(true)});
what Java type would be needed to read t开发者_运维技巧he serialized ajax_form
in a Spring 3 controller?
@RequestMapping("/doajax")
@ResponseBody
public String doAjax(@RequestParam <?Type> ajaxForm
{
// do something
}
First of all, you use form fields without name
s, so serialize()
actually produces an empty result. Add names:
<form>
<input name = "in1" id="in1" type="text" value="one">
<input name = "in2" id="in2" type="text" value="two">
<input name = "in3" id="in3" type="text" value="three">
</form>
I guess you use Prototype, so parameters: $('ajax_form').serialize(true)
produces a URL-encoded representation of the form (and also you don't need true
here, it adds unnecessary conversion). Since @RequestParam
can't bind complex types, you can bind fields as separate parameters:
public String doAjax(@RequestParam("in1") String in1,
@RequestParam("in2") String in2, @RequestParam("in2") String in2)
Also you can create a class to hold form data and pass it as a model attribute:
public class AjaxForm {
private String in1;
private String in2;
private String in3;
... getters, setters ...
}
-
public String doAjax(AjaxForm form)
精彩评论