私のWebアプリケーションにはClearCacheInterceptor
、ログアウトアクションの実行後にキャッチをクリアして、ユーザーが[戻る]ボタンをクリックした場合に前のページが表示されないようにするために含めたいと思います。コードを実行すると正常に動作しますが、ログインして他のアクションを実行できますが、ログアウトをクリックするとエラーが表示されます。これが私のコードです
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;
}
}
ログアウトコード
public String logOut() {
String status = "";
if (true) {
session.clear();
session = null;
if (session instanceof org.apache.struts2.dispatcher.SessionMap) {
try {
((org.apache.struts2.dispatcher.SessionMap) session).invalidate();
} catch (IllegalStateException e) {
}
}
status = "success";
} else {
addActionError(getText("system.ERROR"));
status = "error";
}
return status;
}
私のstruts.xmlアクションファイル
<struts>
<!-- Configuration for the default package. -->
<constant name="struts.Devmode" value="false"></constant>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.custom.i18n.resources" value="ApplicationResources" />
<package name="default" extends="struts-default">
<interceptors>
<interceptor-stack name="defaultStack">
<interceptor-ref name="exception" />
<interceptor-ref name="alias" />
<interceptor-ref name="servletConfig" />
<interceptor-ref name="prepare" />
<interceptor-ref name="i18n" />
<interceptor-ref name="chain" />
<interceptor-ref name="debugging" />
<interceptor-ref name="profiling" />
<interceptor-ref name="scopedModelDriven" />
<interceptor-ref name="modelDriven" />
<interceptor-ref name="fileUpload" />
<interceptor-ref name="checkbox" />
<interceptor-ref name="staticParams" />
<interceptor-ref name="params">
<param name="excludeParams">dojo..*,d-..*,6..*</param>
</interceptor-ref>
<interceptor-ref name="conversionError" />
<interceptor-ref name="validation">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
<interceptor-ref name="workflow">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
</interceptor-stack>
<interceptor name="clear-cache" class="com.java.session.ClearCacheInterceptor" />
</interceptors>
<default-interceptor-ref name="defaultStack"/>
<action name="users">
<result>home.jsp </result>
</action>
<action name="UserAction" class="com.java.action.RegisterAction">
<result name="input">/signup.jsp</result>
<result name="success">/home.jsp</result>
<result name="error">/error.jsp</result>
</action>
<action name="trialregister">
<result>signup.jsp</result>
</action>
<action name="Activate" class="com.java.action.ActivationAction">
<result name="input">/error.jsp</result>
<result name="success">/home.jsp</result>
<result name="error">/test1.jsp</result>
</action>
<action name="verifyLogin" class="com.java.action.LoginAction" method="verifyLogin">
<result name="input">/home.jsp</result>
<result name="success">/test1.jsp</result>
</action>
<action name="logOut" class="com.java.action.LoginAction" method="logOut">
<interceptor-ref name="clear-cache"/>
<result name="error">/error.jsp</result>
<result name="success">/logout.jsp</result>
</action>
<action name="Back">
<result>/index.jsp</result>
</action>
<action name="Reroot">
<result>/index.jsp</result>
</action>
</package>
ログのエラーメッセージは
SEVERE: Servlet.service() for servlet default threw exception
java.lang.NullPointerException
at com.java.action.LoginAction.logOut(LoginAction.java:103)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:404)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:267)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:229)
at com.java.session.ClearCacheInterceptor.intercept(ClearCacheInterceptor.java:37)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:50)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:504)
at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:419)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:864)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1665)
at java.lang.Thread.run(Thread.java:722)