Sunday 16 September 2007

Tip: Put result in session in Spring Web Flow

So, another tip with Spring Web Flow. Sometimes it's necessary put a result of bean-action in scope session, but in SWF 1.X doesn't have this feature, but you can make a simple action to do the work.

First, the flow definition:

<action-state id="saveProject">
<bean-action bean="projectManager" method="save">
<method-arguments>
<argument expression="flowScope.project" />
</method-arguments>
<method-result name="project" scope="request"/>
</bean-action>
<transition on="success" to="putInSession" />
</action-state>

<action-state id="putInSession">
<action bean="putInSessionAction">
<attribute name="sessionKey" value="project" />
</action>
<transition on="success" to="finish"/>
</action-state>


I call a bean-action "saveProject" and I put the result in request with key "project". Next, I execute the action "putInSession" and I set the attribute "sessionKey" with the value of the key "project", this mean thay the object to save in session is in the request with this key.

With this convention, here the Action implementation:

public class PutInSessionAction implements Action{

public Event execute(RequestContext context) throws Exception {

String key = (String) context.getAttributes().get("sessionKey");
Object value = context.getRequestScope().get(key);
context.getExternalContext().getSessionMap().put(key, value);

return new Event(this, "success");
}

}


This simple Action gets the object from the request and puts in session.

5 comments:

Anonymous said...

Cool tip. I'll post a link on the Ervacon SWF Portal.

dahernan said...

Thanks :)

Anonymous said...

Thanks...!!!It is well described.

Anonymous said...

Thank it's helpful
in 2.0 it is more easier
See This

http://forum.springframework.org/showthread.php?p=206734

Anonymous said...

Hi Dear, have you been certainly visiting this site daily, if that's the case you then will certainly get good knowledge.web 2.0 service