浅谈Struts2中的拦截器,Interceptor

        Struts2出来已经有很长时间了,很多关注于java新技术的人都在不断的去了解这个新型的web层框架。自然也少不了我,虽然在看他之前我从一些人口中了解到的资料有好有坏,也有人说这只不过是webwork修改一个名字所得的结果而以,但是我看过之后有众多的优秀特征值得去关注。

 

典型的HelloWorld之类我就不想说了,因为这个东西在网上一找一大片,所以我决定和大家讨论一下java中的拦截器Interceptor,然后再用一个HelloWorld实现。

先来看一段官方文档的一段话:

When you request a resource that maps to an "action", the framework invokes the Action object. But, before the Action is executed, the invocation can be intercepted by another object. After the Action executes, the invocation could be intercepted again. Unsurprisingly, we call these objects "Interceptors."

我理解的意思是当请求一个对应的action的时候,框架会调用这个对应的action对象,但是在这个action执行之气ian,这个调用会被其他的对象拦截(intercepted),在action执行之后,这个调用会被再次拦截(intercepted),显然,这些负责拦截(intercepted)的对象就叫做拦截器(interceptor)

从上面这张图以及apache官方发布的文档来看,interceptor的引进应该是为了在action一层提供一些类似aop模式的一些功能,比如说对于大部分的action在执行之前我们要对它做一些公共的处理,这些已经被struts2封装成了一个默认的interceptor stack;而对于一些action而言,他们则需要一些特殊的处理了,比如说验证,赋值,或者是其他的一些操作。这些操作就要由拦截器来执行了。

当然既然大部分的拦截器功能已经被框架封装成一个拦截器栈了,但是对于一些特殊的功能我们就需要自定义拦截器了。

当然自定义的拦截器首先要实现一个接口com.opensymphony.xwork2.interceptor.Interceptor interface,这个接口定义很简单,只有三个方法

Public void init()

Public void destroy()

Public String intercept(ActionInvocation invocation)

其中init方法是在初始化时调用的,通常是在应用程序启动的时候

Destroy则是在销毁时候调用的

Intercept 是这个接口中比较重要的方法,在这个方法中可以通过invocation,获取一些关于当前执行的action的信息,进而对这个action进行更加细化的工作。

下面来看一个简单的不能再简单的例子:

首先来看一下action

定义接口:

David.IhelloWorld.java

 

package david;

 

public interface IHelloWorld {

         public String execute() throws Exception;

 

         public void setMessage(String message);

 

         public String getMessage();

}

 

实现接口,写个action

David.HelloWorld.java

 

package david;

 

import com.opensymphony.xwork2.ActionSupport;

 

public class HelloWorld extends ActionSupport implements IHelloWorld {

         /**

          *

          */

         private static final long serialVersionUID = -828457125600316166L;

 

         private String message;

         @Override

         public String execute() throws Exception {

                   // TODO Auto-generated method stub

                   System.out.println("action executing...");

                   return SUCCESS;

         }

 

         public void setMessage(String message) {

                   this.message = message;

         }

 

         public String getMessage() {

                   return message;

         }

}

这个action在控制台打印出action executing...字符串。

struts.xml中配置一下action

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC

        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

        "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

    <package name="david" extends="struts-default">

                   <action name="HelloWorld" class="david.HelloWorld">

                            <result>/HelloWorld.jsp</result>

                   </action>

    </package>

</struts>

 

下面实现一个拦截器

David. InterceptorTest.java

 

package david;

 

import com.opensymphony.xwork2.ActionInvocation;

import com.opensymphony.xwork2.interceptor.Interceptor;

 

public class InterceptorTest implements Interceptor {

 

         /**

          *

          */

         private static final long serialVersionUID = -6864587343237650166L;

 

         public void destroy() {

                   // TODO Auto-generated method stub

                   System.out.println("destroy...");

         }

 

         public void init() {

                   // TODO Auto-generated method stub

                   System.out.println("init...");

                  

         }

         public String intercept(ActionInvocation invocation) throws Exception {

                   // TODO Auto-generated method stub

                   System.out.println("before start...");

                   IHelloWorld helloWorld=(IHelloWorld)invocation.getAction();

                   helloWorld.setMessage("Hello World!");

                   String result=helloWorld.execute();

                   System.out.println(helloWorld.getMessage());

                   System.out.println("after start...");                

                   return result;

         }

}

struts.xml中的配置,红体字

这个配置很简单,就是声明一个拦截器,让他去拦截HelloWorld的执行

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC

        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

        "http://struts.apache.org/dtds/struts-2.0.dtd">

 

<struts>

    <package name="david" extends="struts-default">

             <interceptors>

                       <interceptor name="test" class="david.InterceptorTest"></interceptor>

             </interceptors>

                   <action name="HelloWorld" class="david.HelloWorld">                          

                            <interceptor-ref name="test"></interceptor-ref>                  

                            <result>/HelloWorld.jsp</result>                     

                   </action>

    </package>

</struts>

可以看到,在这个拦截器中,我们我们得到这个拦截器所关联的action并且将action的工作进行了细化,包括方法的执行顺序,以及每执行一次的打印到控制台的字符串。

 

之后在页面上我们也输出在拦截器中设置过的message属性

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<%@ taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Hello World</title>

</head>

<body>

<s:property value="message"/>

<br>

</body>

</html>

启动tomcat之后,浏览器输入地址

http://localhost:8080/S2HelloWorld/HelloWorld.action

可在页面看到Hello World

控制台打印

before start...

action start...

Hello World!

after start...

 

至此,拦截器的HelloWorld实例完毕。

拦截器的功能要远比这个大的多,比如说今天看到一个问题说要在action层实现事务控制,那么在struts2中很容易就联想到拦截器了。

关于拦截器的更多知识建议大家去apache的网站上了解一下。

http://struts.apache.org/2.0.11/docs/interceptors.html

结题!

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值