为什么在注销后单击页面上的后退按钮会显示上一页内容? [英] Why after logout clicking back button on the page displays previous page content?

查看:188
本文介绍了为什么在注销后单击页面上的后退按钮会显示上一页内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Struts 2项目。当用户单击注销按钮时,注销操作会使用 session.clear()清除会话。但是当用户在注销后单击浏览器中的后退按钮时,它仍会显示上一页内容。如果在注销后在浏览器中单击后退按钮,我想将用户重定向到登录页面。

I am working on a Struts 2 project. When user clicks a logout button the logout action clears the session using session.clear(). But when user clicks the back button in the browser after logout, it still displays the previous page content. I want to redirect an user to the login page, if the back button was clicked in the browser after logout.

我的退出行动中还有其他什么可以解决这个问题吗?
我们将非常感谢任何帮助。

Is there anything else I should clear in my logout action to solve this problem? Any help will be greatly appreciated.

推荐答案

在您按下后退按钮之前,您的浏览器正在缓存页面。浏览器缓存机制旨在通过从页面具有相同URL获取本地缓存中的页面来最小化服务器访问时间。它可以显着减少数千个客户端浏览服务器时的服务器负载,并且似乎非常有用。但在某些情况下,特别是在您的情况下,内容应该更新。后退按钮的设计使其可以缓存用户正在浏览的每个页面,并在按下后退按钮时从本地缓存中检索它们。因此,解决方案是在返回带有控制浏览器缓存的特殊标头的响应时告诉浏览器不允许缓存。在servlet环境中,您可以使用过滤器来关闭缓存,但在Struts2中,您可以使用自定义拦截器。例如

Turns out that your browser is caching pages before you press the back button. The browser caching mechanism is designed so to minimize the server access time by getting the page from the local cache if the page have the same URL. It significantly reduces the server load when browsing the server by thousands of clients and seems to be very helpful. But in some cases, especially in yours the content should be updated. The back button is designed so it caches every page that a user is browsing and retrieve them from the local cache when pressed the back button. So, the solution is to tell the browser to not allow caching when returning a response with a special headers that control the browser caching. In the servlet environment you might use a filter to turn off caching but in Struts2 you could use a custom interceptor. For example

public class CacheInterceptor implements Interceptor {

private static final long serialVersionUID = 1L;

@Override
public void destroy() {}

@Override
public void init() {}

@Override
public String intercept(ActionInvocation invoication) throws Exception {
    HttpServletRessponse response = ServletActionContext.getResponse();
    response.setHeader("Cache-Control", "no-cache");
    response.setHeader("Pragma", "no-cache");
    response.setHeader("Expires", "-1");
    return invoication.invoke();
}

}

现在你可以配置这个拦截器了每次操作都使用

Now you could configure this interceptor to use by every action

<package name="default" extends="struts-default" abstract="true">

    <interceptors>
      <interceptor name="cache" class="org.yourcompany.struts.interceptor.CacheInterceptor "/>
      <interceptor-stack name="cacheStack">
        <interceptor-ref name="cache"/>
        <interceptor-ref name="defaultStack"/>
      </interceptor-stack>
    </interceptors>
    <default-interceptor-ref name="cacheStack"/>

</package>

当您的包扩展默认包时,他们会继承拦截器和拦截器堆栈,您还可以通过操作配置覆盖此配置。

When your packages extend default package they inherit the interceptor and the interceptor stack, you can also override this configuration by the action configuration.

这篇关于为什么在注销后单击页面上的后退按钮会显示上一页内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆