0

我正在尝试在struts 2中编写小型登录应用程序。会话正在成功创建。在welcome.jsp中给出了“注销”选项。注销控制将被重定向到Logout.jsp。我的问题是在注销会话变量被销毁但页面存储在浏览器缓存中之后。如果单击浏览器的后退按钮,我可以看到welcome.jsp。使用清除缓存“ClearCacheInterceptor”。我不明白我在哪里犯了错误。不是每次都清除浏览器,有什么可以解决这个问题的吗?我的方法正确吗?请给我建议。

登录.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib  uri="/struts-tags"  prefix="s"  %>    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login</title>
</head>
<body>
    <s:form action="login">
        <s:textfield name="myname"></s:textfield>
        <s:submit value="submit"></s:submit>
    </s:form>
</body>
</html> 

Struts.xml

<interceptors>
            <interceptor name="clear-cache"  class="ActionClasses.ClearCacheInterceptor" />
         </interceptors>

        <action name="login" class="ActionClasses.LoginAction" >
           <interceptor-ref name="clear-cache" /> 
           <result name="success">Welcome.jsp</result>
           <result name="error">Login.jsp</result>
        </action>  

        <action name="logout" class="ActionClasses.Logout">
          <interceptor-ref name="clear-cache" /> 
          <result name="success">Logout.jsp</result>
        </action>  

登录操作.java

package ActionClasses;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.validator.annotations.RequiredFieldValidator;
import com.opensymphony.xwork2.validator.annotations.ValidatorType;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class LoginAction extends ActionSupport implements SessionAware
{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private String myname;
    private Map<String , Object> s;

    public String execute()throws Exception
    {

            s=ActionContext.getContext().getSession();
            s.put("login", myname);

         return "success";

   }


    public void setMyname(String s)
    {

        myname=s;

    }

    public String getMyname()
    {
        return myname;
    }


    @Override
    public void setSession(Map<String, Object> arg0) {
        // TODO Auto-generated method stub
        s=arg0;
    }
  }

ClearcacheInterceptor.java

package ActionClasses;





import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.StrutsStatics;



import com.opensymphony.xwork2.ActionContext;

import com.opensymphony.xwork2.ActionInvocation;

import com.opensymphony.xwork2.interceptor.AbstractInterceptor;



public class ClearCacheInterceptor  extends AbstractInterceptor{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Override

    public String intercept(ActionInvocation invocation) throws Exception {

        ActionContext context=(ActionContext)invocation.getInvocationContext();

        HttpServletResponse response=(HttpServletResponse)context.get(StrutsStatics.HTTP_RESPONSE);

        response.setHeader("Cache-Control", "no-cache");

        response.setHeader("Pragma", "no-cache");

        response.setDateHeader("Expires", 0);

        String result=invocation.invoke();

        System.out.println("check result="+result);

        return result;

    }

}

注销.java

package ActionClasses;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;


public class Logout extends ActionSupport  {



    public String execute(){

        Map<String,Object> s=ActionContext.getContext().getSession();



        s.remove("login");



        ActionContext.getContext().getSession().clear();



        return "success";
    }



}

欢迎.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<s:include value="CheckLogin.jsp"></s:include>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
</head>
<body>
<font color="white"></font>
Welcome<s:property value="#session['login']"/>
<s:a href="logout">Logout</s:a>
</body>
</html>

注销.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

log out successful !!


</body>
</html>

检查登录.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page language="java" contentType="text/html" import="java.util.*"%>
<html>
  <head>
  <title>Check validate!</title>
  </head>
  <body>
  This is session validation page!

  <s:if test="#session.login != 'Jagan'">
  <jsp:forward page="Login.jsp" />  
  </s:if>
  </body>
</html>
4

1 回答 1

3

嗯,这是一个非常常见的问题,这与您的浏览器缓存问题有关,而不是 struts2 或任何其他框架。

我们遇到了同样的问题,因为当您点击浏览器的后退按钮时,请求并未发送到服务器,而是从浏览器缓存中提供服务。您只会在尝试做一些工作时注意到事情,它会出现出现您不再登录的错误。

尽管您可以使用某些标头,例如 no-cache 等,但浏览器是否遵守它们并不确定。根据我的理解,解决此问题的唯一方法是在您的工作中使用 https(安全浏览)而不是使用标头(无缓存。缓存到期等),因为当您在安全模式下浏览应用程序时,这些标头后面将跟随服务器和浏览器。

我希望这会给你一个想法,只是为了在你注销时检查重定向到 https 协议,它会解决你的问题

于 2011-07-20T10:50:18.160 回答