- Java code
@SuppressWarnings("serial")public class MyInterceptor extends AbstractInterceptor { @SuppressWarnings("unchecked") @Override public String intercept(ActionInvocation arg0) throws Exception {// HttpSession hs = ServletActionContext.getRequest().getSession();// String username = (String) hs.getAttribute("username");// ActionContext ac = arg0.getInvocationContext(); Map session = ac.getSession(); System.out.println(session.size()); Userinfo userinfo = (Userinfo) session.get("user"); if(userinfo==null){ return Action.LOGIN; }else{ return arg0.invoke(); } }}
------解决方案--------------------
可能是struts配置拦截器的问题,配置例子如下:
- Java code
<interceptors> <interceptor name="myInterceptor" class="cn.com.MyInterceptor"/> <interceptor-stack name="myInterceptorStack"> <interceptor-ref name="defaultStack"/> <interceptor-ref name="myInterceptor"/> </interceptor-stack> </interceptors> <action name="list_*" class="cn.com.action.TheAction" method="{1}"> <!--拦截的TheAction中的方法--> <interceptor-ref name="myInterceptorStack" /> </action>