Maintain state with spring between requests

I'm new to spring, so sorry if this is a newbies question, but the manual is not clear (at least not for me)

My question is: how do I share state between requests in spring? I can send data from the controller to the view using ModelMap, but the data in ModelMap will not be sent back to the next controller using the view. How to do it with spring?

The following is part of my source code. In the second controller, modelMap does not contain the data that I saved in modelMap in the first controller. How should I maintain state between controllers in spring?

Many thanks for the help.

@RequestMapping(value = "find/something", method = RequestMethod.GET)
public String foo(@RequestParam("parent") Parent parent, ModelMap modelMap) {    
...
    modelMap.addAttribute("question_index", 42);
    modelMap.addAttribute("something", new Something());
    modelMap.addAttribute("data", new Data());
    return "some/view";
}

<form:form action="bla" method="POST"  modelAttribute="data">
...// using Something() and 42
</form:form>

@RequestMapping(value = "bla", method = RequestMethod.POST)    
public String bla(@ModelAttribute("data") Data data, BindingResult result, ModelMap modelMap) {
System.out.println(modelMap); // doesn't contain question_index, or something
}
+5
source share
5

modelMap HttpSession ( ), Spring -, .

+6

Spring - Spring bean . bean Spring . bean .

HttpSession, , HttpSession , .

Spring ( ) .

+4

Thank you very much for your suggestions, I solved this by marking the keys for ModelMap as session attributes:

@SessionAttributes( { "question_index", "something" })  
@Controller  
public class MyController{  
...  
}  
+1
source

Typically (and without Spring) such data will flow into the servlet session.

0
source

do it yourself.

<form:form action="bla" method="POST"  modelAttribute="data">
    <input type="hidden" name="data" value="${data}"/>
</form:form>
-1
source

All Articles