4.Struts2的拦截器

l          自定义的拦截器的方法有三种:

  1. 实现Interceptor接口(实现intercept)

  注意:intercept的参数ActionInvocation必须调用invoke方法后才后继续执行下面的拦截器,直到所有的拦截器都执行完后再执行所拦截action的业务方法,最后执行下面的程序,类似一个栈,所以有拦截栈的说法。其返回的字符串即为action类的业务方法返回的值

 package edu.yzu.interceptor;

import com.opensymphony.xwork2.ActionInvocation;

import com.opensymphony.xwork2.interceptor.Interceptor;

 

@SuppressWarnings("serial")

public class FirstInterceptor implements Interceptor {

 

    private String paramOne;

    private String paramTwo;

    public String getParamOne() {

        return paramOne;

    }

 

    public void setParamOne(String paramOne) {

        this.paramOne = paramOne;

    }

    public String getParamTwo() {

        return paramTwo;

    }

    public void setParamTwo(String paramTwo) {

        this.paramTwo = paramTwo;

    }

 

    public void destroy() {

    }

    public void init() {

    }

    public String intercept(ActionInvocation actionInvocation) throws Exception {

        System.out.println(paramOne+" do before in FirstInterceptor!");

        String result=actionInvocation.invoke();//调用下一级拦截器

        System.out.println(paramTwo+" do after in FirstInterceptor!");

        return result;

    }

 

}

 

 

  1. 继承AbstractInterceptor抽象类(一般会这么用)

 

package edu.yzu.interceptor;

 

import com.opensymphony.xwork2.ActionInvocation;

import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

 

@SuppressWarnings("serial")

public class SecondInterceptor extends AbstractInterceptor {

 

    @Override

    public String intercept(ActionInvocation actionInvocation) throws Exception {

        System.out.println("do before in SecondInterceptor!");

        String result=actionInvocation.invoke();

        System.out.println("do after in SecondInterceptor!");

        return result;

    }

 

}

  1. 继承MethodFilterInterceptor实现doInterceptor(用得也比较多)

package edu.yzu.interceptor;

 

import com.opensymphony.xwork2.ActionInvocation;

import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;

 

@SuppressWarnings("serial")

public class ThirdInterceptor extends MethodFilterInterceptor {

 

    @Override

    protected String doIntercept(ActionInvocation actionInvocation) throws Exception {

        System.out.println("do before in ThreeInterceptor!");

        String result= actionInvocation.invoke();

        System.out.println("do after in ThreeInterceptor!");

        return result;

    }

 

}

l          拦截器的配置

<?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>

<!-- 下面这个标签可以改变struts的默认配置,包括使用编码

国际化名称,以及为文件上传时临时设置的保存目录等。这在default.properties中可以找到

 -->

<constant name="struts.custom.i18n.resources" value="messages,information"></constant>

 

<!--

struts2的包机制,很好用,struts-default 在struts的jar包里面struts-default.xml有定义

 -->

 

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

 

<interceptors>

<!-- 配置拦截器,拦截器的执行先后顺序与配置相同,当然这与指定时有关系

每个拦截器可以为有set方法的属性赋值,方法如下

 -->

<interceptor name="firstinterceptor" class="edu.yzu.interceptor.FirstInterceptor">

<param name="paramOne">Jack</param>

<param name="paramTwo">Rose</param>

</interceptor>

 

 

<interceptor name="secondinterceptor" class="edu.yzu.interceptor.SecondInterceptor">

</interceptor>

 

<!--

可以指定要拦截的方法:

如下:若是不配置则默认拦截所有的方法

若是一个方法即被包含,又被排除,则此方法会被拦截。

这种配置可以在声明这个拦截器时指定,也可以在调用此拦截器时指定,

所谓调用即指定其指定此拦截器为哪个action调用!

注意:只在声明并不会调用些拦截器

 

 -->

<interceptor name="thirdinterceptor" class="edu.yzu.interceptor.ThirdInterceptor">

<param name="excludeMethods">update</param>

<param name="includeMethods">register</param>

</interceptor>

 

<!--

拦截器栈,struts2本质上将它与拦截器等同,不过配置点有点区别,里面可以有多个拦截器,并且也可以有多个拦截器栈,

且里面的栈也一样

 -->

 

 <interceptor name="rightinterceptor" class="edu.yzu.interceptor.RightInterceptor"></interceptor>

<interceptor-stack name="mystack">

<interceptor-ref name="firstinterceptor">

</interceptor-ref>

<interceptor-ref name="secondinterceptor">

</interceptor-ref>

<interceptor-ref name="defaultStack">

</interceptor-ref>

</interceptor-stack>

</interceptors>

 

<action name="register" class="edu.yzu.action.UserAction" method="register">

<!-- result 的name属性可以不写,不写的话表示为success -->

<result>/result.jsp</result>

<result name="input">/index.jsp</result>

<result name="login">/login.jsp</result>

<!--

为这个action指定拦截器,若不指定刚会使用此包的默认拦截器

若指定一个拦截器,则此包的默认拦截器则不会被用于拦截些action

所以若还要使用默认拦截器时必须显示指出,如下:

 -->

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

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

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

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

</action>

 

 

<action name="upload" class="edu.yzu.action.FileUpLoadAction">

<result>/loadok.jsp</result>

<result name="input">/upload.jsp</result>

<interceptor-ref name="fileUpload">

<!-- 多个允许类型可用逗号分开,源码可查!

如果一个不被允许的文件上传,则后引发错误,这个错误消息也放在filederror里面,

一旦发生这个错误,则会转回到input对应的路径上去!这些错误的消息并不太好处理,

所以跟类型转换出现错误一样,struts2使此类错误可以国际化,以方便配置,及按需显示。

这些配置可以在struts-messages.properties中找到,然后在自己的国际化文件中配置即可

如上传文件类型不被允许的错误为:struts.messages.error.content.type.not.allowed

可以在国际化文件中这么配置:struts.messages.error.content.type.not.allowed=file type is error!以指定自己的

想要显示的消息!

这个action配置了两次fileUpload这个拦截器,这并不会引发错,但会没有这个必要。

拦截器配置几次执行几次拦截,并且拦截器的执行顺序与配置顺序一致

 -->

<param name="allowedTypes">application/msword</param>

</interceptor-ref>

<!-- 一旦一个action显示配置了一个拦截器,则默认拦截器将不起作用,如果需要则要显示引入 -->

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

</action>

 

</package>

  

 

</struts>

posted @ 2010-05-03 16:59  沉兮  阅读(613)  评论(0编辑  收藏  举报