拦截器代码import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.AbstractInterceptor;import java.util.Map;/** * Created by allme on 2016/5/25. */public class LoginInterceptor extends AbstractInterceptor { @Override public String intercept(ActionInvocation actionInvocation) throws Exception { try{ Map session = actionInvocation.getInvocationContext().getSession(); String username = (String) session.get("username"); String userpwd = (String) session.get("userpwd"); if("".equals(username)||"".equals(userpwd)||username==null||userpwd==null){ System.out.println("what "+username); System.out.println("what "+userpwd); return "login"; } else{ return actionInvocation.invoke(); } }catch (Exception e){ e.printStackTrace(); }finally { return null; } }}配置文件<?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> <package name="register" extends="struts-default"> <action name="useraction" class="cn.edu.zucc.action.UserAction"> <result name="showuser">/showuser.jsp</result> </action> </package> <package name="login" extends="struts-default"> <interceptors> <interceptor name="Login" class="cn.edu.zucc.interceptot.LoginInterceptor" /> <interceptor-stack name="loginstack"> <interceptor-ref name="defaultStack"></interceptor-ref> <interceptor-ref name="Login"></interceptor-ref> <interceptor-ref name="timer"></interceptor-ref> </interceptor-stack> </interceptors>
1 回答
繁星coding
TA贡献1797条经验 获得超4个赞
如果已经运行到你的System.out.println("what "+username);部分,那确实证明拦截器已经正确工作了,但最后没有跳到login页面?
死马当活马医吧,试试调整下拦截器的使用顺序?
<action name="Hello" class="cn.edu.zucc.action.HelloAction">
<interceptor-ref name="loginstack"></interceptor-ref>
<result name="showuser">/showuser.jsp</result>
<result name="login">/login.jsp</result>
</action>
修正:
最终原因@allme 自己已找到,是finally
里return null;
的缘故。
添加回答
举报
0/150
提交
取消