用戶登錄後,會添加用戶的會話。當用戶點擊註銷時,它首先在動作前通過攔截器。我不明白爲什麼我的會話在它繞過我的CacheInterceptor後爲空。STRUTS2中我的SESSION的NullPointerException
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一部分到達,它停止,因爲空指針異常的執行。
您是否在會話中設置了屬性「USER」? – 2014-12-02 05:53:19
是的,當用戶在應用程序中登錄時,我已經設置了USER會話。 – Rjmr 2014-12-02 05:54:30
您是否嘗試過打印這個'<%if(session.getAttribute(「USER」)== null){%><%}%>'以確保您的會話中包含了它 –
2014-12-02 05:56:41