用户登录拦截器

来源:互联网 发布:iphone移动数据不能用 编辑:程序博客网 时间:2024/04/30 02:23

自定义用户拦截器类;

import java.util.Map;

import com.opensymphony.xwork2.*;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class LoginInterceptor extends AbstractInterceptor{
public String intercept(ActionInvocation ai) throws Exception{
                       //ActionInvocation是通过框架传递过来,通过它可得session对象
Map session = ai.getInvocationContext().getSession();
                      //获取session对象
String username = (String)session.get("user");
if(username != null && username.length() > 0){
return ai.invoke();//进行后续操作,执行下一些列拦截器
}else{
ActionContext ac = ai.getInvocationContext();
                  //获取ActionContext对象
ac.put("popedom", "你还没有登录");
                  //设置提示信息
return Action.LOGIN;
}
}

}

登录拦截器配置:struts.xml

<interceptors>
    <interceptor name="check" class="mypkg.LoginInterceptors"/>
    <interceptor-stack name="checkLogin"><!--定义一个拦截器栈-->
          <interceptor-ref name="check"/>
          <interceptor-ref name="defaultStack"/>
    </interceptor-stack>
</interceptors>
<default-interceptor-ref name="checkLogin"/><!--使用默认拦截器-->
<action name="" class=""></action>

原创粉丝点击