首先在登陆action中 session赋值“Login_user” ,在拦截器类中取出session 用户,如果存在,跳转到action;不存在跳回login.jsp。
LoginInterceptor.java
public class LoginInterceptor extends AbstractInterceptor {//继承AbstractInterceptor类
@Override
public String intercept(ActionInvocation arg0) throws Exception {
Map session = arg0.getInvocationContext().getSession();
User user = (User) session.get("Login_user");
if(user != null){
return arg0.invoke();
} else {
return Action.LOGIN;
}
}
}
配置xml, struts.xml <packet>中
<interceptors>
<interceptor name="checkLogin" class="com.interceptor.LoginInterceptor" />
<interceptor-stack name="myInterceptor" >
<interceptor-ref name="checkLogin" />
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="myInterceptor" />
<global-results> //设置跳转路径
<result name="login" type="redirect">/login.jsp</result>
<result name="error">/error.jsp </result>
</global-results> 这样,当前包下所有的action,都会经过拦截。

365

被折叠的 条评论
为什么被折叠?



