<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 调试模式-->
<constant name="struts.devMode" value="true"></constant>
<package name="p1" extends="struts-default">
<!--声明一个登录拦截器-->
<interceptors>
<interceptor name="BOSLoginIntercepter" class="com.gyf.bos.web.interceptor.BOSLoginInterceptor">
<!--excludeMethods排除:login不需要拦截,往后的register也不需要-->
<param name="excludeMethods">login</param>
</interceptor>
<!--声明一个拦截器组(栈)-->
<interceptor-stack name="MyDefaultStack">
<interceptor-ref name="BOSLoginIntercepter"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<!--修改struts默认拦截器-->
<default-interceptor-ref name="MyDefaultStack"/>
<!--配置全局的结果视图-->
<global-results>
<result name="login">/login/jsp</result>
</global-results>
<!-- 配置jsp页面的访问规则-->
<action name="page_*_*" >
<result name="success">/WEB-INF/pages/{1}/{2}.jsp</result>
</action>
<!--用户模块 com.gyf.bos.web.action.UserAction -->
<action name="userAction_*" class="com.gyf.bos.web.action.UserAction" method="{1}">
<result name="home">/WEB-INF/pages/common/index.jsp</result>
<result name="loginfailure">/login.jsp</result>
</action>
</package>
</struts>
package com.gyf.bos.web.interceptor;
import com.gyf.bos.model.User;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
import org.apache.struts2.ServletActionContext;
public class BOSLoginInterceptor extends MethodFilterInterceptor {
/**
* string返回的是action的方法返回值
*/
@Override
protected String doIntercept(ActionInvocation actionInvocation) throws Exception {
//是否登录
User user = (User) ServletActionContext.getRequest().getSession().getAttribute("loginUser");
System.out.println("拦截了.." + ServletActionContext.getRequest().getRequestURL());
if (user != null){
return actionInvocation.invoke();
}
return "login";//回到登录页面,配置一个全局的result
}
/*
* 1.写个登录拦截器
* 2.在struts.xml配置一个登录拦截器
* 3.配置一个拦截器分组,把登录拦截器和struts默认拦截器添加进组
* 4.修改struts默认拦截器
* 5.在登录拦截器排除一些不需要拦截的方法excludeMethods
* */
}