现在的位置: 首页 > 综合 > 正文

框架技术–struts2拦截器与自定义拦截器使用

2014年09月28日 ⁄ 综合 ⁄ 共 5240字 ⁄ 字号 评论关闭

博文转载:http://www.cnblogs.com/withyou/p/3170440.html

拦截器可谓struts2的核心了,最基本的bean的注入就是通过默认的拦截器实现的,一般在struts2.xml的配置中,package内直接或间接继承了struts-default.xml,这样struts2默认的拦截器就会作用.下面详细的说明一下:

Interceptor拦截器类似于过滤器,是可以在action执行前后执行的代码。是我们做web开发时经常用的技术。比如:权限控制、日志等。我们也可以将多个Interceptor连在一起组成Interceptor栈。

Struts2拦截器,每个拦截器类只有一个对象实例,即采用单例模式,所以引用这个拦截器的Action都共享这一拦截器类的实例,因此,在拦截器中如果使用类变量,要注意同步问题。

实现原理 : Struts2拦截器的实现原理相对简单,当请求struts2的action时,Struts 2会查找配置文件,并根据其配置实例化相对应拦截器对象,然后串成一个列表,最后一个一个地调用列表中的拦截器。

拦截器和过滤器的区别:

1. 拦截器和过滤器的概念非常类似。 

2. 过滤器隶属于web容器,可以过滤一切请求(包括action、servlet、jsp、html等等)。

3. 而拦截器隶属于struts2框架,只能拦截action(无法拦截对jsp的请求)。 

4. 过滤器内部采用函数回调来实现。拦截器采用动态代理来实现!

拦截器在struts2中的应用: 

对于Struts2框架而言,正是大量的内置拦截器完成了大部分操作。比如: 

 – 像params拦截器将http请求中参数解析出来赋值给Action中对应的属性。 

 – Servlet-config拦截器负责把请求中HttpServletRequest实例和HttpServletResponse实例传递给Action

 struts-default.xml中有一个默认的引用,在默认情况下(也就是<action>中未引用拦截器时)会自动引用一些拦截器。

常用拦截器:token

作用:防止表单重复提交拦截器

使用方法:配置struts2.xml+重复提交处理+jsp页面表单内插入<s:token></s:token>标签;其中struts.xml的配置是核心

(1)先来看一下最基本的struts.xml的配置:

<package name="test" namespace="/" extends="struts-default">
        <action name="testValidate" class="com.bjsxt.struts.test.TestValidateAction">
         <interceptor-ref name="token"></interceptor-ref>
             <result name="success">/ok.jsp</result>
             <result name="input">/testFormLabel.jsp</result>
             <result name="invalid.token">/tokenInvalid.jsp</result>
        </action>
</package>

说明:

1.代码第三行代表引入一个struts2自带的拦截器token,此操作会使默认的拦截器失效,即无法使用其他功能,自定义拦截器再详细说明;

2.代码第六行为基本固定的格式,result的name属性值是固定的,后面的/tokenInvalid.jsp可以根据需求自己定义

3.此token拦截器会对该action下的所有请求作用,最关键的一点!即:所有该action的请求都会经过token的拦截处理,自定义拦截器时再说明;

(2)form表单中需加入<s:token></s:token>标签:

 <s:form action="testValidate" >
         <s:textfield name="uname" ></s:textfield>
         <s:token></s:token>        
         <s:submit></s:submit>
 </s:form>

说明:不一定是s标签的表单,普通表单中插入<s:token></s:token>也可以实现拦截的效果.

自定义拦截器:

作为“框架(framework)”,可扩展性是不可或缺的,因为世上没有放之四海而皆准的东西。虽然,Struts 2为我们提供如此丰富的拦截器实现,但是这并不意味我们失去创建自定义拦截器的能力,恰恰相反,在Struts 2自定义拦截器是相当容易的一件事。

大家在开始着手创建自定义拦截器前,切记以下原则:
拦截器必须是无状态的,不要使用在API提供的ActionInvocation之外的任何东西。要求拦截器是无状态的原因是Struts 2不能保证为每一个请求或者action创建一个实例,所以如果拦截器带有状态,会引发并发问题。

 条件:

1. 直接或间接实现接口com.opensymphony.xwork2.interceptor.Interceptor或者继承类com.opensymphony.xwork2.interceptor.AbstractInterceptor 

2.通过<interceptor>元素来定义拦截器 

3.通过<interceptor-ref>元素来使用拦截器

4.自定拦截器的java类并重写public String intercept(ActionInvocation ai) throws Exception

注意:如果为Action指定了一个拦截器,则系统默认的拦截器栈将会失去作用。为了继续使用默认拦截器,所以上面配置文件中手动引入了默认拦截器

 步骤:

1.新建一个class,用于实现自定义拦截器功能,这儿就实现最为普遍的登陆验证,即防止未经登陆的情况下,访问action;先说一下原理:

首先,用户登陆后,经过action验证用户名和密码是否正确,如果正确则在session作用域内存放user对象,否则跳到登陆页面并提示用户名或密码不正确,而且session作用域中user的值是没有设置的.然后,由于在该action下使用了自定义的拦截器(struts.xml配置),访问该action的所有请求都会被拦截,验证session中user属性的值,如果值不存在,则跳到登陆页面并提示无权操作,否则执行invoke方法,即请求会被action正常响应,并返回相应的结果.

代码体现如下:

自定义拦截器类:

package com.bjsxt.struts2.exercise.interceptor;


import java.util.Map;


import com.bjsxt.struts2.exercise.vo.Users;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;


public class LoginInterceptor extends MethodFilterInterceptor{
    @Override
    protected String doIntercept(ActionInvocation invocation) throws Exception {
        //获取session对象(经过struts2包装过)
        Map session = ActionContext.getContext().getSession();
        //获取session作用域内是否有值
        Users user = (Users) session.get("user");
        if(user!=null){//合法访问
            return invocation.invoke();
        }else{//user为空说明未经过登陆,保存错误提示信息,跳到登陆页面
            ActionContext.getContext().put("noright", "请先登陆再进行操作!");
            return Action.LOGIN;
        }
    }
}

2.配置struts.xml,使自定义拦截器作用:

<package name="move" namespace="/move" extends="struts-default">
        <interceptors>
            <interceptor name="loginInteceptor" class="com.bjsxt.struts2.exercise.interceptor.LoginInterceptor"></interceptor>
        </interceptors>
        <global-results>
            <result name="login" >/jsp/movebooking/login.jsp</result>
        </global-results>
        <action name="moveBookingAction" class="com.bjsxt.struts2.exercise.action.MoveBookingAction" >
            <interceptor-ref name="loginInteceptor">
                <param name="excludeMethods">addMoveInfo</param>
            </interceptor-ref>
            <interceptor-ref name="token">
                <param name="includeMethods">addMoveInfo</param>
            </interceptor-ref>
            <interceptor-ref name="defaultStack"></interceptor-ref>
            <result name="queryList" >/jsp/movebooking/movelist.jsp</result>
            <result name="queryInfo" >/jsp/movebooking/moveinfo.jsp</result>
            <result name="update" >/jsp/movebooking/updateinfo.jsp</result>
            <result name="afterupdate" type="redirectAction" >moveBookingAction!queryMoveList</result>
            <result name="success">/jsp/movebooking/success.jsp</result>
            <result name="invalid.token">/jsp/movebooking/tokenInvalid.jsp</result>
        </action>
        
        <action name="userAction" class="com.bjsxt.struts2.exercise.action.UserAction">
            <result name="success" type="redirectAction" >moveBookingAction!queryMoveList</result>
            <result name="failed" type="redirect">/jsp/movebooking/login.jsp
            </result>
        </action>
    </package>

说明:

  (1)使用自定义拦截器会使默认拦截器失效,所以需要手动引入:代码第15行;当然上述代码引入拦截器的方式也可以采用拦截器栈的方式,就不贴了;

  (2)注意拦截器的顺序,一般情况下,默认拦截器都是放在最后面的,权限验证与token相比较,我觉得先验证是否登陆更加必要;

  (3)interceptor-ref内可以定义param元素,即实现对特定方法的拦截;但是注意,使用方法拦截器必须直接或间接实现AbstractInerceptor接口或者继承MethodFilterInterceptor类,并重写doIntercept方法.param标签内有两个属性:

      excludeMethods:排除的方法

      includeMethods:包含的方法

在本配置文件中,由于token拦截器的作用范围为moveBookingAction下所有请求,而我在测试的时候就发现,该action下的所有请求都会被拦截到重复提交的错误页面,原理尚不明白,有高手敬请指导(只有需要被作用的请求对应的jsp页面的form表单内加入了token标签),所以被逼无奈对token拦截的方法只限定在addMoveInfo方法中!

抱歉!评论已关闭.