开发者

How to set all post data in ModelMap in Spring MVC?

I have many input fields in a form to post data to server side. And in controller, I want to get all attributes in ModelMap like this:

public String save(ModelMap model) {
    ....
}

I don't want to use @RequestParam since there are so many fields to be sent. I th开发者_StackOverflow中文版ink the data in a form will be posted and saved in ModelMap automatically, but its not working

Could anyone help me? Thanks.


You should use a @ModelAttribute in the form handler

@RequestMapping(value="/submitform", method = RequestMethod.POST)
public String save(@ModelAttribute("mydata") MyData myData) {

 //do something with
 //myData.getField1();
 //myData.getFiled2();
}

and this is how you will send the page to the form

@RequestMapping("/fillform")
public String loadForm(ModelMap model) {

  //you could also fill MyData, to do autofill on the html form
  model.put("mydata", new MyData());

  return "fillform"; //[jsp]view resolver will pick up fillform.jsp 
 }


I am trying to make the same thing. The problem with previous answer is taht the UI is dynamically build by iterating on Map<String, Object>. So it is not possible to pre define the MyData.class.

I am solving this issue using next approach:

@RequestMapping(value = "/secure/filled", method = RequestMethod.POST) public String saveFilledData(HttpServletRequest request, ModelMap model) { request.getParameter("nameOfTheParameter"); }

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜