目录
拦截器的创建
创建方式1
实现Interceptor接口
package cn.ycsj.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
//拦截器,创建方式1
//拦截器的生命周期:随项目的启动而创建,随项目的关闭而销毁 (servlet 第一次访问创建 项目关闭销毁)
public class MyInterceptor1 implements Interceptor {
/**
* 销毁
*/
@Override
public void destroy() {
}
/**
* 初始化
*/
@Override
public void init() {
}
/**
* 拦截方法
* @param actionInvocation
* @return
* @throws Exception
*/
@Override
public String intercept(ActionInvocation actionInvocation) throws Exception {
return null;
}
}
创建方式2
继承AbstractInterceptor
package cn.ycsj.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
//拦截器,创建方式2
//继承AbstractInterceptor
//拦截器的生命周期:随项目的启动而创建,随项目的关闭而销毁 (servlet 第一次访问创建 项目关闭销毁)
public class MyInterceptor2 extends AbstractInterceptor {
@Override
public String intercept(ActionInvocation actionInvocation) throws Exception {
return null;
}
}
创建方式3(推荐)
继承MethodFilterInterceptor
package cn.ycsj.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
//拦截器,创建方式3
//继承MethodFilterInterceptor
//功能定制拦截器拦截的方法。
// 定制哪些方法需要拦截,
// 定制哪些方法不需要拦截。
public class MyInterceptor3 extends MethodFilterInterceptor {
@Override
protected String doIntercept(ActionInvocation Invocation) throws Exception {
//前处理
System.out.println("MyInterceptor3 前处理 。。。。");
//放行
Invocation.invoke();
//后处理
System.out.println("MyInterceptor3 后处理 。。。。");
return null;
}
}
放行Invocation.invoke();
不放行 不执行Invocation.invoke(); return "xxx"; 就会不执行后续的拦截器以及action 直接跳转跳过页面
拦截器的配置
1.创建一个拦截器
package cn.ycsj.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
//拦截器,创建方式3
//继承MethodFilterInterceptor
//功能定制拦截器拦截的方法。
// 定制哪些方法需要拦截,
// 定制哪些方法不需要拦截。
public class MyInterceptor3 extends MethodFilterInterceptor {
@Override
protected String doIntercept(ActionInvocation Invocation) throws Exception {
//前处理
System.out.println("MyInterceptor3 前处理 。。。。");
//放行
String result = Invocation.invoke();
//后处理
System.out.println("MyInterceptor3 后处理 。。。。");
return result;
}
}
2.配置拦截器
参考struts-default.xml文件
在struts.xml中配置
<package name="results" namespace="/" extends="struts-default">
<interceptors>
<!-- 1.注册拦截器-->
<interceptor name="MyInterceptor3" class="cn.ycsj.interceptor.MyInterceptor3"></interceptor>
<!-- 2.注册拦截器栈-->
<interceptor-stack name="myStack">
<!--自定义的拦截器引入 (建议放在20个拦截器之前 ) -->
<interceptor-ref name="MyInterceptor3"/>
<!--需要添加默认拦截器栈 引用默认的拦截器栈(20个) -->
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
<!-- 3.指定包中默认拦截器栈-->
<default-interceptor-ref name="myStack"/>
也可以在单独的action中配置 指定只有某个action走拦截器(配置的时候就不需要指定包中的拦截器栈)
<action name="UserAction_*" class="cn.ycsj.struts.UserAction" method="{1}">
<!--为单独的action执行走哪个拦截器(栈)-->
<!-- <interceptor-ref name="myStack"/>-->
<result name="tohome" type="redirect">
/index.jsp
</result>
<result name="error">/login.jsp</result>
</action>
拦截器拦截方法指定,定制拦截器
1.创建一个action
package cn.ycsj.struts;
import com.opensymphony.xwork2.ActionSupport;
public class Demo1Action extends ActionSupport {
public String add() throws Exception {
System.out.println("demo1_add");
return SUCCESS;
}
public String delete() throws Exception {
System.out.println("demo1_delete");
return "xxx";
}
public String update() throws Exception {
System.out.println("demo1_update");
return SUCCESS;
}
public String select() throws Exception {
System.out.println("demo1_select");
return SUCCESS;
}
}
2.配置struts.xml中的拦截器
注意不拦截配置<param name="excludeMethods">add,delete</param>
和拦截配置<param name="includeMethods"></param>不能同时使用 ,只能二选一
<package name="results" namespace="/" extends="struts-default">
<interceptors>
<!-- 1.注册拦截器-->
<interceptor name="MyInterceptor3" class="cn.ycsj.interceptor.MyInterceptor3"></interceptor>
<!-- 2.注册拦截器栈-->
<interceptor-stack name="myStack">
<!--自定义的拦截器引入 (建议放在20个拦截器之前 ) -->
<interceptor-ref name="MyInterceptor3">
<!--指定哪些方法不拦截 拦截和不拦截不能同时使用 -->
<param name="excludeMethods">add,delete</param>
<!--指定拦截 <param name="includeMethods"></param> -->
</interceptor-ref>
<!--需要添加默认拦截器栈 引用默认的拦截器栈(20个) -->
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
<!-- 3.指定包中默认拦截器栈-->
<default-interceptor-ref name="myStack"/>
<action name="Demo1Action_*" class="cn.ycsj.struts.Demo1Action" method="{1}">
<result name="success" >
/index.jsp
</result>
<result name="xxx">/form2.jsp</result>
</action>
</package>
<!--配置为true,热加载配置文件,日志信息更加详细-->
<constant name="struts.devMode" value="true"/>
</struts>
访问控制台结果如下
demo1_delete
MyInterceptor3 前处理 。。。。
demo1_select
MyInterceptor3 后处理 。。。。
验证登录拦截器的实现
拦截器创建
public class LoginInterceptor extends MethodFilterInterceptor {
/**
* 指定不拦截登录方法,其他的方法都需要拦截登录
* @param invocation
* @return
* @throws Exception
*/
@Override
protected String doIntercept(ActionInvocation invocation) throws Exception {
//1.获取session
Map<String, Object> session = ActionContext.getContext().getSession();
//2.获得登录标识
Object user = session.get("user");
//3.判断登录标识是否存在
//不存在=>没登录=>重定向到登录界面
//存在=>已经登录 =>放行
if (user ==null){
return "toLogin";
}else {
return invocation.invoke();
}
}
}
配置
<?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>
<package name="results" namespace="/" extends="struts-default">
<interceptors>
<!-- 1.注册拦截器-->
<interceptor name="loginInterceptor" class="cn.ycsj.interceptor.LoginInterceptor"></interceptor>
<!-- 2.注册拦截器栈-->
<interceptor-stack name="myStack">
<!--自定义的拦截器引入 (建议放在20个拦截器之前 ) -->
<interceptor-ref name="loginInterceptor">
<!--指定哪些方法不拦截 拦截和不拦截不能同时使用 -->
<param name="excludeMethods">add,delete</param>
<!--指定拦截 <param name="includeMethods"></param> -->
</interceptor-ref>
<!--需要添加默认拦截器栈 引用默认的拦截器栈(20个) -->
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
<!-- 3.指定包中默认拦截器栈-->
<default-interceptor-ref name="myStack"/>
<!--定义全局结果集 -->
<global-results>
<result name="toLogin" type="redirect">/login.jsp </result>
</global-results>
<!--如果出现RuntimeException 这个异常 就将跳转到名为error的结果 需要配置结果集 -->
<global-exception-mappings>
<exception-mapping exception="java.lang.RuntimeException" result="error"></exception-mapping>
</global-exception-mappings>
<action name="UserAction_*" class="cn.ycsj.struts.UserAction" method="{1}">
<!--为单独的action执行走哪个拦截器(栈)-->
<!-- <interceptor-ref name="myStack"/>-->
<result name="tohome" type="redirect">
/index.jsp
</result>
<result name="error">/login.jsp</result>
<result name="success">/form2.jsp</result>
</action>
</package>
<!--配置为true,热加载配置文件,日志信息更加详细-->
<constant name="struts.devMode" value="true"/>
</struts>
全局结果集的定义省去了在每个action中都需要配置result的烦恼
<global-results>
<result name="toLogin" type="redirect">/login.jsp </result>
</global-results>