English 中文(简体)
maintain state with spring between requests
原标题:

I am new to spring so sorry if this is a beginners 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 by using a ModelMap, but the data in the ModelMap is not sent back to the next controller by the view. How can I do this with spring?

Below is a part of my source code. In the second controller the modelMap doesn t contain the data I stored in the modelMap in the first controller. How am I supposed to maintain state between controllers in spring?

thanks a lot for 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
}
问题回答

You can either put the modelMap in the HttpSession, or (preferable for larger applications), use Spring web flow where you can have the so called conversations.

The cleanest way to do this in Spring is with a session-scoped spring bean. Instances of the bean will be private to the session, and will be instantiated and managed by Spring when each session starts. This bean can hold your conversation state.

Under the covers, this mechanism uses standard HttpSession attributes, but it means your code doesn t have to deal with HttpSession directly, so it s cleaner overall.

See the relevant part of the Spring docs (and here) for how to configure and use it.

thanks a lot for your suggestions, I solved it by tagging the keys for the ModelMap as session-attibutes:

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

Typically (and without Spring), such data would go into the servlet session.

do it yourself.

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




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签