ppb
ppb

Reputation: 2613

How to maintain Struts2 Session

I am using Struts2 in my application. In my application I have one login form and one main page. On successful login, I call main page and inside that main page I have called another action. Up to this all is working fine and page is also display after successful login but when I press F5 or click on browser refresh button it will give me login page but I want the same page on which I fire F5 or refresh button. How can I achieve this, my struts.xml is as follow..

...

<action name="mytable" class="MyDataTable">

        <result name="success" type="json"/>            
        <result name="error">messages.jsp</result>

    </action>
    <action name="edit" class="MyEditAction">

        <result name="input" type="json"></result>
        <result name="success" type="json"></result>
        <result name="error">messages.jsp</result> 

    </action>
    <action name="Login" class="LoginAction">

        <result name="input">login.jsp</result>
        <result name="success">main.jsp</result>            
        <result name="error">messages.jsp</result>

    </action>
    <action name="Logout" class="LogoutAction"> 
        <result name="success">login.jsp</result>
    </action>

...

After login I call main.jsp and from main.jsp I am calling mytable action. After showing me the main page the address bar of browser still shows me the Login action

So please help me how can I maintain session.

Upvotes: 0

Views: 1318

Answers (1)

Hugo
Hugo

Reputation: 496

First of all, you must consider using a security framework. I recommend using Spring Security. There is A LOT that you can do wrong implementing security all by yourself. This will make sure users are properly authenticated and will guard you from many attacks. From the code you give us, it doesn't seem that you have this in place.

For your scenario, you want to use the so-called Post/Redirect/Get pattern. This pattern will make sure that the browser is redirected to a GET url, after doing a post. This will make sure that the user does not submit a form twice. It also makes sure that the browseris pointed to the new url.

To do this in Struts2, you must use the Redirect Action Result. An example is also worked-out here.

In your case this would be something like:

...
<action name="Login" class="LoginAction">

    <result name="input">login.jsp</result>
    <result name="success" type="redirect">
        <param name="actionName">displaySalesOrder</param>
        <param name="namespace">/yournamespace</param>
    </result>            
    <result name="error">messages.jsp</result>

</action>

<action name="main" class="MainAction"> 
    <result name="success">main.jsp</result>
</action>
...

Upvotes: 2

Related Questions