一、拦截器的三种创建方式
拦截器有三种创建方式:
实现Interceptor接口
继承AbstractInterceptor
继承MethodFilterInterceptor
需要说明的是 :
AbstractInterceptor 是 Interceptor 的实现类
MethodFilterInterceptor 是 AbstractInterceptor 的子类
1.1 实现Interceptor接口
1.1.1 Interceptor接口
public interface Interceptor extends Serializable {
/**
* Called to let an interceptor clean up any resources it has allocated.
*/
void destroy();
/**
* Called after an interceptor is created, but before any requests are processed using
* {@link #intercept(com.opensymphony.xwork2.ActionInvocation) intercept} , giving
* the Interceptor a chance to initialize any needed resources.
*/
void init();
/**
* Allows the Interceptor to do some processing on the request before and/or after the rest of the processing of the
* request by the {@link ActionInvocation} or to short-circuit the processing and just return a String return code.
*
* @param invocation the action invocation
* @return the return code, either returned from {@link ActionInvocation#invoke()}, or from the interceptor itself.
* @throws Exception any system-level error, as defined in {@link com.opensymphony.xwork2.Action#execute()}.
*/
String intercept(ActionInvocation invocation) throws Exception;
}
1.1.2 代码示例
package com.qwm.s4.a_create;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
/**
* @author:qiwenming
* @date:2017/9/23 0023 22:59
* @description:
* 拦截器的第一种创建方式 实现接口
*/
public class MyInterceptor1 implements Interceptor{
@Override
public void destroy() {
}
@Override
public void init() {
}
@Override
public String intercept(ActionInvocation actionInvocation) throws Exception {
return null;
}
}
1.2 继承AbstractInterceptor
1.2.1 AbstractInterceptor类
public abstract class AbstractInterceptor implements Interceptor {
/**
* Does nothing
*/
public void init() {
}
/**
* Does nothing
*/
public void destroy() {
}
/**
* Override to handle interception
*/
public abstract String intercept(ActionInvocation invocation) throws Exception;
}
1.2.2 代码示例
package com.qwm.s4.a_create;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
/**
* @author:qiwenming
* @date:2017/9/23 0023 23:00
* @description:
* 拦截器的第二种创建方式 继承 AbstractInterceptor
*/
public class MyInterceptor2 extends AbstractInterceptor{
@Override
public String intercept(ActionInvocation actionInvocation) throws Exception {
return null;
}
}
1.3 继承MethodFilterInterceptor
1.3.1 MethodFilterInterceptor
public abstract class MethodFilterInterceptor extends AbstractInterceptor {
private static final Logger LOG = LogManager.getLogger(MethodFilterInterceptor.class);
protected Set<String> excludeMethods = Collections.emptySet();
protected Set<String> includeMethods = Collections.emptySet();
public void setExcludeMethods(String excludeMethods) {
this.excludeMethods = TextParseUtil.commaDelimitedStringToSet(excludeMethods);
}
public Set<String> getExcludeMethodsSet() {
return excludeMethods;
}
public void setIncludeMethods(String includeMethods) {
this.includeMethods = TextParseUtil.commaDelimitedStringToSet(includeMethods);
}
public Set<String> getIncludeMethodsSet() {
return includeMethods;
}
@Override
public String intercept(ActionInvocation invocation) throws Exception {
if (applyInterceptor(invocation)) {
return doIntercept(invocation);
}
return invocation.invoke();
}
protected boolean applyInterceptor(ActionInvocation invocation) {
String method = invocation.getProxy().getMethod();
// ValidationInterceptor
boolean applyMethod = MethodFilterInterceptorUtil.applyMethod(excludeMethods, includeMethods, method);
if (!applyMethod) {
LOG.debug("Skipping Interceptor... Method [{}] found in exclude list.", method);
}
return applyMethod;
}
/**
* Subclasses must override to implement the interceptor logic.
*
* @param invocation the action invocation
* @return the result of invocation
* @throws Exception in case of any errors
*/
protected abstract String doIntercept(ActionInvocation invocation) throws Exception;
}
1.3.2 代码示例
package com.qwm.s4.a_create;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
/**
* @author:qiwenming
* @date:2017/9/23 0023 22:59
* @description:
* 拦截器的第三种创建方式 继承
*/
public class MyInterceptor3 extends MethodFilterInterceptor{
@Override
protected String doIntercept(ActionInvocation actionInvocation) throws Exception {
return null;
}
}
二、准备一个测试的Action
2.1 DemoAction
package com.qwm.s4.b_test;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* @author:qiwenming
* @date:2017/9/23 0023 23:29
* @description:
* 测试
*/
public class DemoAction extends ActionSupport{
public String add(){
System.out.println("-----add------");
ActionContext.getContext().put("msg","--add--");
return SUCCESS;
}
public String delete(){
System.out.println("-----delete------");
ActionContext.getContext().put("msg","--delete--");
return SUCCESS;
}
public String update(){
System.out.println("-----update------");
ActionContext.getContext().put("msg","--update--");
return SUCCESS;
}
public String find(){
System.out.println("-----find------");
ActionContext.getContext().put("msg","--find--");
return SUCCESS;
}
}
2.2 message.jsp
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2017/9/23 0023
Time: 23:39
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>信息显示</title>
</head>
<body>
<h1>
Hellow,wiming<br>
${msg}
</h1>
</body>
</html>
2.3 struts.xml
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="intertest" namespace="/" extends="struts-default">
<!--全局允许方法调用-->
<global-allowed-methods>regex:.*</global-allowed-methods>
<!--动态方法调用-->
<action name="demoAction_*" class="com.qwm.s4.b_test.DemoAction" method="{1}">
<result name="success">/message.jsp</result>
</action>
</package>
</struts>
2.4 图示
三、使用拦截器
创建拦截器的时候,往往使用 上面的创建方式3,这里我们使用的也是方式3
3.1 MyInterceptor
package com.qwm.s4.b_test;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
/**
* @author:qiwenming
* @date:2017/9/23 0023 23:12
* @description:
* 拦截器使用
*/
public class MyInterceptor extends MethodFilterInterceptor{
@Override
protected String doIntercept(ActionInvocation invocation) throws Exception {
//前处理
System.out.println("-----MyInterceptor------前处理");
//放行 ,如果不放行,也可以直接放回结果
String resultCode = invocation.invoke();
System.out.println("-----MyInterceptor------后处理");
return resultCode;
}
// @Override
// protected String doIntercept(ActionInvocation invocation) throws Exception {
// //不放行,也可以直接放回结果
// return "success";
// }
}
3.2 配置 struts.xml
配置可以查看默认配置 struts2-core-2.5.13.jar!\struts-default.xml。
配置分为三个步骤:
注册拦截器
注册拦截器栈
指定包中的默认拦截器栈
下面的配置中,我们拦截了 add 和 delete 方法
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="intertest" namespace="/" extends="struts-default">
<!--1.注册拦截器-->
<interceptors>
<interceptor name="myinterceptor" class="com.qwm.s4.b_test.MyInterceptor"/>
<!-- 2.注册拦截器栈 -->
<interceptor-stack name="myStack">
<!-- 自定义拦截器引入(建议放在20个拦截器之前) -->
<interceptor-ref name="myinterceptor">
<!-- 指定哪些方法不拦截
<param name="excludeMethods">add,delete</param> -->
<!-- 指定哪些方法需要拦截 -->
<param name="includeMethods">add,delete</param>
</interceptor-ref>
<!-- 引用默认的拦截器栈(20个) -->
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<!-- 3.指定包中的默认拦截器栈 -->
<default-interceptor-ref name="myStack"></default-interceptor-ref>
<!--全局允许方法调用-->
<global-allowed-methods>regex:.*</global-allowed-methods>
<!--动态方法调用-->
<action name="demoAction_*" class="com.qwm.s4.b_test.DemoAction" method="{1}">
<!-- 为Action单独指定走哪个拦截器(栈) -->
<!--<interceptor-ref name="myStack"/>-->
<result name="success">/message.jsp</result>
</action>
</package>
</struts>