Announcement Announcement Module
Collapse
No announcement yet.
Accessing session variables from web flow action Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Accessing session variables from web flow action

    I have been testing the latest web flows and I must say what an excellent piece of work! Iíve been replacing a Spring MVC wizard using a single FormAction subclass and validator Ö the code is more compressed than a Zip archive.

    Since the intent of web flow appears to move the programmer further away from the servlet container, I have a question regarding the best way to obtain session variables. I currently use the following snippit, which works, but again I would just like to confirm:

    Code:
     HttpServletRequest request = ((HttpServletRequestEvent) context.getOriginatingEvent()).getRequest();
    Thanks,
    Seamus

  • #2
    That is indeed the correct way to get to the HttpRequest and HttpSession. There is ofcourse a tradeof: doing this kind of thing makes your Action HTTP Servlet specific, so it can't be used unchanged in e.g. a Portlet scenario. If you would keep the Actions protocol independent (like the PhoneBook sample app), you can reuse your action and flow for different protocols: servlets, portlets, ...

    If you are only building servlet based web-apps, there is ofcourse no real problem in accessing the HTTP session in this way. Still, it could be a good idea to introduce a SessionHelper or something that abstracts access to the session.

    Erwin

    Comment


    • #3
      Hi Erwin!

      I appreciate the guidance for accessing session variables, however I'm lost in the implementation.

      In SimpleFormController, each method was given the HttpServletRequest making the call to the method unique to the request. In other words, the method was self contained with knowledge of its surroundings.

      In web flow, each method is passed a RequestContext, but how do I inject knowledge into that context so that it's still unique to the request and not tied to servlets?

      Thanks,
      Seamus

      Comment


      • #4
        Seamus,

        The RequestContext has a "requestScope" and "flowScope" property of type Scope.

        e.g.

        Code:
        context.getRequestScope().setAttribute("name", "value");
        context.getFlowScope().setAttribute("name", "value");

        Comment


        • #5
          Hi Kevin!

          I am aware of the two scopes, but if the action and flow are to remain implementation-neutral, then how do I give the action or flow knowledge of an object which is stored in the session? Erwin suggested a SessionHelper to abstract access to the session. Where does this helper get introduced into the flow?

          I spent some time looking at the HttpServletFlowExecutionManager, which I could subclass and inject my session variable into the request scope. Then I noticed the FlowExecutionListener as another point to inject into the request scope. Either of these would be set via beans and that would keep both the flow and action implementation-neutral.

          Or is there a better method?

          Thanks,
          Seamus

          Comment


          • #6
            A listener seems like a good idea! Thanks for the tip!

            You just need to implement requestSubmitted() to pull the info from the HTTP session and put it in request scope (or flow scope if required). Your flows and actions remain protocol independent and it is easy to swap out the listener if you want to use your flows with another protocol (e.g. portlets).

            Erwin

            Comment

            Working...
            X