Bharat
Bharat

Reputation: 11

Reload the page on hitting back button

I am using Struts2 for my project. Working on simple registration project. When user Login , he is directed to his profile, but on hitting browser's back button login page appears again. XML:

<action name="home" class="bharat.Home">
            <result name="success">/jsp/home.jsp</result>
             <result name="student">/jsp/Welcome.jsp</result>
</action>
<action name="Login" class="bharat.LoginAction">
            <result name="input">/index.jsp</result>
            <result name="success">/jsp/Welcome.jsp</result>
</action>

Home.java:

public String execute() {
           if((String)ActionContext.getContext().getSession().get("studentName")!=null)
        {
            return "student";
        }
        if((String) ActionContext.getContext().getSession().get("instructorName")!=null)
        {
            return "instructor";
        }
        session = ActionContext.getContext().getSession();
        session.put("roleName","notAdmin");
        return SUCCESS;
    }

Home.jsp

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta name="cache-control" content="no-cache" />
<meta name="expires" content="0" />
<meta name="pragma" content="no-cache" />
    <sx:head/>

I want that when user hit back button after being logged in , he must be redirected back to same page. Can anyone Help me please..

Upvotes: 0

Views: 1826

Answers (3)

Bharat
Bharat

Reputation: 11

<script type="text/javascript">
    function fn()
        {   
                 window.open("WelcomeDirect.action","_self");               
        }
</script>
</head>
<body onLoad="fn()">
Reloading.....
</body>

redirecting page

Upvotes: -1

Umesh Awasthi
Umesh Awasthi

Reputation: 23587

You can use post and redirect method where all you need to when user login he/she should be redirected to welcome page. If you see banking sites or any other secure site they have deployed the same mechanism and it work perfectly fine.

Secondly i suggest not to play with ActionContext as Struts2 has given you a more clean and flexible way to implement SessionAware interface, by implementing this your Action is free from underlying framework dependencies and easy to test

Upvotes: 0

mnmnc
mnmnc

Reputation: 374

I understand that the current flow looks like this:

User connects to URL -> provides login on login page -> loading first page

Do you consider changing the flow to the following:

User connect to URL -> provides login on login page -> goes to page with autoredirect to first page -> loading first page.

This way when user hist back button from 'first page' it will get the redirect page and automaticaly get back to the 'first page'.

Upvotes: 2

Related Questions