自定义拦截器
提供工具类: /** * 工具类 * */ public class BOSUtils { //获取session对象 public static HttpSession getSession(){ return ServletActionContext.getRequest().getSession(); } //获取登录用户对象 public static User getLoginUser(){ return (User) getSession().getAttribute("loginUser"); } }` /** * 自定义的拦截器,实现用户未登录自动跳转到登录页面 * */ public class BOSLoginInterceptor extends MethodFilterInterceptor{ //拦截方法 protected String doIntercept(ActionInvocation invocation) throws Exception { //从session中获取用户对象 User user = BOSUtils.getLoginUser(); if(user == null){ //没有登录,跳转到登录页面 return "login"; } //放行 return invocation.invoke(); } } <!--在struts.xml中配置--> <interceptors> <!-- 注册自定义拦截器 --> <interceptor name="bosLoginInterceptor" class="com.itheima.bos.web.interceptor.BOSLoginInterceptor"> <!-- 指定哪些方法不需要拦截 --> <param name="excludeMethods">login</param> </interceptor> <!-- 定义拦截器栈 --> <interceptor-stack name="myStack"> <interceptor-ref name="bosLoginInterceptor"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </interceptor-stack> </interceptors> <default-interceptor-ref name="myStack"/> <!-- 全局结果集定义 --> <global-results> <result name="login">/login.jsp</result> </global-results>