Struts2拦截器配置实例

来源:互联网 发布:mysql 随机取一条记录 编辑:程序博客网 时间:2024/05/21 19:47

拦截器介绍
拦截器 的使用 ,源自Spring AOP(面向切面编程)思想
拦截器 采用 责任链 模式
* 在责任链模式里,很多对象由每一个对象对其下家的引用而连接起来形成一条链。
* 责任链每一个节点,都可以继续调用下一个节点,也可以阻止流程继续执行

在struts2 中可以定义很多个拦截器,将多个拦截器按照特定顺序 组成拦截器栈 (顺序调用 栈中的每一个拦截器 )

1、 struts2 所有拦截器 都必须实现 Interceptor 接口
2、 AbstractInterceptor 类实现了 Interceptor 接口. 并为 init, destroy 提供了一个空白的实现

所有实际开发中,自定义拦截器 只需要 继承 AbstractInterceptor类, 提供 intercept 方法实现

3、 常用struts2 拦截器
模型驱动
文件上传
参数解析封装
类型转换错误
请求参数校验
拦截跳转 input 视图
自定义拦截器案例
案例 : 登陆,对其它Action访问 通过自定义拦截器 进行权限控制
导入jar包 (struts2 jar、c3p0、 dbutils、 mysql驱动)
web.xml
struts.xml
JDBCUtils 工具类

第一步 : 编写index.jsp 提供 图书增删改查 四个功能
编写BookAction ,提供四个业务方法

第二步: 完成登陆功能

第三步 :必须要登陆 才能进行图书管理
使用Filter 进行权限控制 —- 过滤所有web请求 (所有web资源访问)
使用拦截器 进行权限控制 —- 主要拦截对Action访问 (不能拦截JSP)

定义拦截器 继承AbstractInterceptor

配置拦截器
方式一










方式二








    <!-- 设置当前包 所有Action 都使用 自定义拦截器栈 -->    <default-interceptor-ref name="privilegeStack"></default-interceptor-ref>


下面是我自己实现的时候练手的操作
第一个是struts.xml

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"    "http://struts.apache.org/dtds/struts-2.3.dtd"><struts>    <constant name="struts.enable.DynamicMethodInvocation" value="false" />    <constant name="struts.devMode" value="true" />    <!-- Add packages here -->    <package name="user" namespace="/" extends="struts-default">        <interceptors>            <interceptor name="bookInterceptor" class="com.action.BookInterceptor">                <param name="includeMethods">add,update,delete</param>                <param name="excludeMethods">search</param>            </interceptor>            <interceptor-stack name="myStack">                <interceptor-ref name="bookInterceptor"></interceptor-ref>                <interceptor-ref name="defaultStack"></interceptor-ref>            </interceptor-stack>        </interceptors>        <global-results>            <result name="login" type="dispatcher">/login.jsp</result>        </global-results>        <action name="login" class="com.action.LoginAction">            <result name="success" type="redirect">/book.jsp</result>        </action>        <action name="book*" class="com.action.BookAction" method="{1}">            <interceptor-ref name="myStack"></interceptor-ref>        </action>    </package></struts>

需要注意的是
1、struts标签下只能有
Content Model : ((package | include | bean | constant)*, unknown-handler-stack?)
2、

Element : packageContent Model : (result-types?, interceptors?, default-interceptor-ref?, default-action-ref?, default-class- ref?, global-results?, global-exception-mappings?, action*) Element : interceptorsContent Model : (interceptor | interceptor-stack)+

3、还有就是interceptor的位置,要在action之前
默认的拦截器也不能忘了引入,
在对应的action里面引入拦截器



下面是BookInterceptor.java的内容,定义了拦截器对请求的操作

package com.action;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.Action;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;public class BookInterceptor extends MethodFilterInterceptor {    @Override    protected String doIntercept(ActionInvocation arg0) throws Exception {        // TODO Auto-generated method stub        User user = (User) ServletActionContext.getRequest().getSession()                .getAttribute("user");        System.out.println("执行了这个拦截器");        if (user == null) {            ServletActionContext.getRequest().setAttribute("msg",                    new String("权限不足,请您先登陆"));            return Action.LOGIN;        }        return arg0.invoke();    }}
0 0