【发布时间】:2014-12-02 05:50:35
【问题描述】:
用户登录后,添加用户的会话。当用户点击注销时, 它在动作之前首先通过拦截器。我不明白为什么我的会话绕过我的 CacheInterceptor 后为空。
CacheInterceptor 的目的是避免用户在退出应用程序后点击返回按钮。
鉴于以下代码:
welcome.jsp
<%@ page language="java" contentType="text/html; charset=US-ASCII"
pageEncoding="US-ASCII"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%if(session.getAttribute("USER") == null){%> <jsp:forward page="login.html"/> <%}%>
<!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=US-ASCII">
<title>Welcome Page</title>
</head>
<body>
<h3>Welcome <s:property value="userName"></s:property></h3>
<form action="logout" method="post">
<input value="Logout" type="submit" name="ibm-submit" class="ibm-btn-arrow-pri" />
</form>
</body>
</html>
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.convention.result.path" value="/"></constant>
<package name="user" namespace="/" extends="struts-default">
<interceptors>
<interceptor name="authentication" class="com.ibm.eaylportal.interceptors.AuthenticationInterceptor"></interceptor>
<interceptor name="noCache" class="com.ibm.eaylportal.interceptors.CacheInterceptor"></interceptor>
<interceptor-stack name="authStack">
<interceptor-ref name="authentication"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
<interceptor-stack name="authStack">
<interceptor-ref name="noCache"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="authStack"></default-interceptor-ref>
<global-results>
<result name="login" type="redirect">/login.html</result>
</global-results>
<action name="home">
<interceptor-ref name="defaultStack"></interceptor-ref>
<result>/login.html</result>
</action>
<action name="login" class="com.ibm.eaylportal.actions.LoginAction">
<interceptor-ref name="defaultStack"></interceptor-ref>
<result name="success">/welcome.jsp</result>
<result name="input">/login.html</result>
</action>
<action name="logout" class="com.ibm.eaylportal.actions.LoginAction" method="logout">
<interceptor-ref name="noCache"></interceptor-ref>
<result>/login.html</result>
</action>
<action name="welcome" class="com.ibm.eaylportal.actions.WelcomeAction">
<result name="success">/welcome.jsp</result>
</action>
</package>
</struts>
这只是我的 ActionClass 的一部分
LoginAction.java
public String logout(){
sessionAttributes.remove("USER");
return SUCCESS;
}
CacheInterceptor.java
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("Interceptor: Cache");
HttpServletResponse response = ServletActionContext.getResponse();
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");
response.setHeader("Expires", "-1");
return invocation.invoke();
}
当程序执行到达该 sessionAttributes.remove 部分时,由于 NullPointerException,它会停止执行。
【问题讨论】:
-
你在会话中设置
USER属性吗? -
是的,我已经在用户登录应用程序时设置了 USER 会话。
-
您是否尝试过打印此
<%if(session.getAttribute("USER") == null){%> <jsp:forward page="login.html"/> <%}%>以确保您在会话中拥有它 -
是的。在这行代码中
Welcome <s:property value="userName"></s:property>userName 打印用户的用户名。
标签: java jsp session struts2 interceptor