struts2拦截器不起作用解决办法
struts2拦截器不起作用
struts.xml 中
<interceptors>
<interceptor name="login" class="com.lz.interceptor.CheckLoginInterceptor"/>
<interceptor-stack name="teamwareStack">
<interceptor-ref name="login"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
实现类:
package com.lz.interceptor;
import java.util.Map;
import com.lz.action.UsersAction;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class CheckLoginInterceptor extends AbstractInterceptor {
public static final String LOGIN_KEY = "login";
@Override
public String intercept(ActionInvocation arg0) throws Exception {
Object action = arg0.getAction();
if (action instanceof UsersAction) {
return arg0.invoke();
}
Map<String, Object> session = arg0.getInvocationContext().getSession();
String login = (String)session.get(LOGIN_KEY);
if(login != null && login.length() > 0){
return arg0.invoke();
} else{
return "error";
}
}
}
------解决方案--------------------
楼主虽然定义了这个拦截器但并没有在struts.xml里面声明使用这个拦截器
加上这句<default-interceptor-ref name="teamwareStack" />将你所定义的拦截器栈变为默认的拦截器栈
------解决方案--------------------
return "success";
struts.xml 中
<interceptors>
<interceptor name="login" class="com.lz.interceptor.CheckLoginInterceptor"/>
<interceptor-stack name="teamwareStack">
<interceptor-ref name="login"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
实现类:
package com.lz.interceptor;
import java.util.Map;
import com.lz.action.UsersAction;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class CheckLoginInterceptor extends AbstractInterceptor {
public static final String LOGIN_KEY = "login";
@Override
public String intercept(ActionInvocation arg0) throws Exception {
Object action = arg0.getAction();
if (action instanceof UsersAction) {
return arg0.invoke();
}
Map<String, Object> session = arg0.getInvocationContext().getSession();
String login = (String)session.get(LOGIN_KEY);
if(login != null && login.length() > 0){
return arg0.invoke();
} else{
return "error";
}
}
}
------解决方案--------------------
楼主虽然定义了这个拦截器但并没有在struts.xml里面声明使用这个拦截器
加上这句<default-interceptor-ref name="teamwareStack" />将你所定义的拦截器栈变为默认的拦截器栈
------解决方案--------------------
return "success";