NetUI Page Flows: An Evolution of Struts
Pages: 1, 2, 3, 4, 5

Session Data Management

The programming framework  provided by Page Flows adds many enhancements, and you've just seen an example of the annotation-driven automatic generation and synchronization of XML configuration files. Let's now turn to another advantage: It is easier to manage and maintain data across actions using Page Flow-scoped variables.

In Struts, action classes get session objects and set attributes (or put values) to be retrieved by other actions later. Listing 4 shows an example of two Struts action classes, LoginAction and LogOutAction, sharing a variable username using the HTTP session.

Listing 4. LoginAction.java and LogOutAction.java

package comparision.struts;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public final class LoginAction extends Action {
 
    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response)
        throws Exception {
        String username = form.getUserName();
        // get this session
        HttpSession session = request.getSession();
         
                        
session.setAttribute("username",  username);
        
        return (mapping.findForward("login"));
    }
}

package comparision.struts;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public final class LogOutAction extends Action {
 
    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response)
        throws Exception {
        
        // get this session
        HttpSession session = request.getSession();
         
                        
String username = session.getAttribute("username");
        //logic to log out user
        return (mapping.findForward("logout"));
    }
}
                      

The above code clearly shows how the username variable must be explicitly shared by LoginAction and LogOutAction, in this case using the HTTP session. Managing such session variables across numerous action classes can get quite complex.

In contrast, Page Flows offer a simple variable declaration at the JPF level that shares data across all Page Flow actions. In the example in Listing 5, a String variable, username, is declared at Page Flow level and is used in the login and logout actions.

Listing 5. LoginPageFlowController.java
package comparision.pageflows;
import org.apache.beehive.netui.pageflow.PageFlowController;
import org.apache.beehive.netui.pageflow.Forward;
import org.apache.beehive.netui.pageflow.annotations.Jpf;

public class LoginPageFlowController extends PageFlowController
{
     
                        
private String username;
    @Jpf.Action(
        forwards = { 
            @Jpf.Forward(name = "success", path = "/index.jsp")
        }
    )
    protected Forward begin()
    {
       //intial processing
       return new Forward("success");
    }

    @Jpf.Action(
        forwards = { 
            @Jpf.Forward(name = "success", path = "/mypage.jsp"),
            @Jpf.Forward(name = "failure", path = "/index.jsp")
        }
    )
    protected Forward login(ProfileForm form)
    {
         
                        
username = form.getUsername();
        //authentication logic
        if(user_authenticated)
        return new Forward("success");
        else return new Forward("failure")
    }

    @Jpf.Action(
        forwards = { 
            @Jpf.Forward(name = "success", path = "/confirm.jsp")
        }
    )
    protected Forward signup()
    {
        return new Forward("success");
    }

    @Jpf.Action(
        forwards = { 
            @Jpf.Forward(name = "success", path = "/begin.do")
        }
    )    
    protected Forward logOut(ProfileForm form)
    {
         
                        
logout(username);
        return new Forward("success");
    }
}
                      

As you can see, the handling of the shared variable is intuitive and straightforward.

Pages: 1, 2, 3, 4, 5

Next Page »