How to preserve the ModelMap between form submits?

Go To


I am adding to the ModelMap a Map which contains a list to go on a drop down an I am populating the ModelAttribute when the form initialises:

@RequestMapping(method = RequestMethod.GET)
    public String initForm(HttpServletRequest httpRequest, ModelMap model)
        model.addAttribute("myList", myMap);
        return "MyForm";

It works as expected however when the user submits the form this list is lost from the map.

@RequestMapping(method = RequestMethod.POST, value ="/dosearch")
    public String processSearch(... ModelMap model)
    return new ModelAndView("MyForm",model);

This approach above doesn't work.

How can I prserve the map between form submits?

2012-04-04 20:58
by Joly


No. Even a flash scope won't help you here, because you are not using redirect-after-post. Here are the options:

  • output all values in the html form, then they will be submitted to /dosearch and you'll be able to get them from the request
  • use the session to store the values (worse option)
2012-04-04 22:31
by Bozho
Thanks. Can you please show me a code example of the first option? What about spring:bind - Joly 2012-04-04 23:28
Also I am happy to consider redirect after post. Can you please elaborate - Joly 2012-04-04 23:34
it serves a different purpos - Bozho 2012-04-05 06:23
So I'm supposed to dump all the values to the html in dynamic hidden fields? sounds a bit messy to me. Any concrete code example to support that - Joly 2012-04-05 09:55
yes, that's what you should do. It's messy, but http is stateless protocol - you can't keep things persistent across requests, unless you use the sessio - Bozho 2012-04-05 11:25