Struts2通过注解来实现拦截器
首页写一个自定义的拦截器类MyInterceptor,实现Interceptor接口(或者继承自AbstractInterceptor)的类。
Interceptor接口声明了三个方法:
public interface Interceptor extends Serializable { void destroy(); void init(); String intercept(ActionInvocation invocation) throws Exception;}nit方法在拦截器类被创建之后,在对Action镜像拦截之前调用,相当于一个post-constructor方法,
使用这个方法可以给拦截器类做必要的初始话操作。
Destroy方法在拦截器被垃圾回收之前调用,用来回收init方法初始化的资源。
Intercept是拦截器的主要拦截方法,如果需要调用后续的Action或者拦截器,只需要在该方法中调用
invocation.invoke()方法即可,在该方法调用的前后可以插入Action调用前后拦截器需要做的方法。
如果不需要调用后续的方法,则返回一个String类型的对象即可,例如Action.SUCCESS。
另外AbstractInterceptor提供了一个简单的Interceptor的实现,这个实现为:
public abstract class AbstractInterceptor implements Interceptor { public void init() { } public void destroy() { } public abstract String intercept(ActionInvocation invocation) throws Exception;}在不需要编写init和destroy方法的时候,只需要从AbstractInterceptor继承而来,实现intercept方法即可。
我们尝试编写一个拦截器,该拦截器,代码为:
package com.sunny.action;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.Interceptor;public class MyInterceptor implements Interceptor {/** * */private static final long serialVersionUID = 1L;@Overridepublic void init() {System.out.println("init");}@Overridepublic String intercept(ActionInvocation invoker) throws Exception {System.out.println("intercept");String result = invoker.invoke();return result;}@Overridepublic void destroy() {System.out.println("destory");}}然后定义在struts.xml中注册拦截器
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"><struts><!-- 请求参数的编码方式 --><constant name="struts.i18n.encoding" value="UTF-8" /><package name="custom-default" extends="struts-default"><interceptors><interceptor name="annotationInterceptor" /><interceptor-stack name="annotatedStack"><interceptor-ref name="annotationInterceptor" /><interceptor-ref name="defaultStack" /></interceptor-stack></interceptors> <!-- 设置全局 全局默认的拦截器栈--> <default-interceptor-ref name="annotatedStack"></default-interceptor-ref> </package></struts>
最后在Action类中通过注解引用
package com.sunny.action;import org.apache.struts2.convention.annotation.Action;import org.apache.struts2.convention.annotation.ExceptionMapping;import org.apache.struts2.convention.annotation.ExceptionMappings;import org.apache.struts2.convention.annotation.InterceptorRef;import org.apache.struts2.convention.annotation.InterceptorRefs;import org.apache.struts2.convention.annotation.Namespace;import org.apache.struts2.convention.annotation.ParentPackage;import org.apache.struts2.convention.annotation.Result;import com.opensymphony.xwork2.ActionSupport;import com.sunny.entity.Users;@ParentPackage("custom-default")@Namespace("/login")// 公共异常捕获@ExceptionMappings({ @ExceptionMapping(exception = "java.lang.Exception", result = "exception") })// 拦截器@InterceptorRefs({ @InterceptorRef("annotatedStack") })public class LoginAction extends ActionSupport {/** * */private static final long serialVersionUID = 1L;private Users users;public Users getUsers() {return users;}public void setUsers(Users users) {this.users = users;}@Action(value = "add", results = {@Result(name = "success", location = "/a.jsp"),@Result(name = "error", location = "/index.jsp") })@Overridepublic String execute() throws Exception {if (users.getUsername().equals("sunny")&& users.getPassword().equals("sunny")) {return "success";} else {return "error";}}}完成后启动服务就可以看到自定义的拦截器输出init
在浏览器中输入:http://localhost:8080/Struts2/login/add.action调用execute()后会看到输出intercept。之后停止服务会看到输出destory。
一个简单的拦截器已经配置完成。
不过我有个疑惑的地方是现在暂时还不是非常明白这个会用在项目中的什么地方,某个功能处。望大家讨论。