Struts Series(6) - 拦截器

来源:互联网 发布:python分词 统计词频 编辑:程序博客网 时间:2024/05/17 21:38

拦截器

概述

Intercetor, 即为拦截器。
1. 在Struts2中,把每一个功能都用一个个的拦截器实现;用户想用struts的哪个功能的时候,可以自由组装使用。
2. Struts2中,为了方法用户对拦截器的引用,提供了拦截器栈的定义,里面可以包含多个拦截器。 文件夹(文件, 文件2) 拦截器栈(拦截器,拦截器2)
3. Struts2中,如果用户没有指定执行哪些拦截器,struts2有一个默认执行的栈,defaultStack;
一旦如果用户有指定执行哪些拦截器,默认的拦截器栈就不会被执行

拦截器的设计,就是基于组件设计的应用.

拦截器配置举例

struts-default.xml文件中,定义了struts提供的所有拦截器.

//1. 定义拦截器以及拦截器栈<interceptors>    1.1 拦截器定义    <interceptor name="" class="" />     1.2 拦截器栈的定义    <interceptor-stack name="defaultStack">    引用了上面拦截器(1.1)    </interceptor-stack></interceptors>2. 默认执行的拦截器(栈)<default-interceptor-ref name="defaultStack"/>

API

名称 解释 Interceptor 拦截器接口 AbstractInterceptor 拦截器默认实现的抽象类; 一般用户只需要继承此类即可继续拦截器开发 ActionInvocation 拦截器的执行状态,调用下一个拦截器或Action

自定义一个拦截器案例

/** * 自定义拦截器 * */public class HelloInterceptor implements Interceptor{    // 启动时候执行    public HelloInterceptor(){        System.out.println("创建了拦截器对象");    }    // 启动时候执行    @Override    public void init() {        System.out.println("执行了拦截器的初始化方法");    }    // 拦截器业务处理方法 (在访问action时候执行? 在execute之前执行?)    @Override    public String intercept(ActionInvocation invocation) throws Exception {        System.out.println("2. 拦截器,业务处理-开始");        // 调用下一个拦截器或执行Action  (相当于chain.doFilter(..)        // 获取的是: execute方法的返回值        String resultFlag = invocation.invoke();        System.out.println("4. 拦截器,业务处理-结束");        return resultFlag;    }    @Override    public void destroy() {        System.out.println("销毁....");    }}

拦截器在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>    <package name="hello" extends="struts-default">        <!-- 【拦截器配置】 -->        <interceptors>            <!-- 配置用户自定义的拦截器 -->            <interceptor name="helloInterceptor" class="xxx.xxx.HelloInterceptor"></interceptor>            <!-- 自定义一个栈: 要引用默认栈、自定义的拦截器 -->            <interceptor-stack name="helloStack">                <!-- 引用默认栈 (一定要放到第一行)-->                <interceptor-ref name="defaultStack"></interceptor-ref>                <!-- 引用自定义拦截器 -->                <interceptor-ref name="helloInterceptor"></interceptor-ref>            </interceptor-stack>        </interceptors>        <!-- 【执行拦截器】 -->        <default-interceptor-ref name="helloStack"></default-interceptor-ref>        <!-- Action配置 -->        <action name="hello" class="xxx.xxx.HelloAction">            <result name="success"></result>        </action>    </package></struts>

拦截器执行流程

启动:
创建所有拦截器、执行init()
访问:
先创建Action,
再执行拦截器,
最后:拦截器放行,执行execute();
UML:

Created with Raphaël 2.1.0用户用户Tomcat服务器Tomcat服务器拦截器1拦截器1拦截器2拦截器2Action实例Action实例1.启动:执行所有拦截器创建2.init()执行所有初始化方法3.init()4.用户访问5.创建Action实例6.拦截器interceptor()方法7.invoke()8.invoke()9.execute()10111213.最终,由服务器响应浏览器

拦截器案例

需求:
登陆后,显示列表。
案例准备:
Struts jar文件
DbUtils组件
数据库连接池/ 驱动包
设计:登录页面login.jsp,当验证成功后跳转到list.jsp,试图直接访问列表页时通过过滤器返回到登录页面。

UserAction.java

import java.util.List;import jp.yuudachi.domain.Admin;import jp.yuudachi.service.AdminService;import jp.yuudachi.service.impl.AdminServiceImpl;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;public class UserAction extends ActionSupport{    //----------1.封装请求数据    private Admin admin;    public Admin getAdmin(){        return admin;    }    public void setAdmin(Admin admin) {        this.admin = admin;    }    //----------2.调用的Service    private AdminService adminService = new AdminServiceImpl();    /**     * 登录     * @return     */    public String login(){        try {            Admin userInfo = adminService.login(admin);            if(userInfo == null){                return "input";            }            ActionContext.getContext().getSession().put("userInfo", userInfo);            return "loginSuccess";        } catch (Exception e) {            return ERROR;        }    }    /**     * 列表     * @return     */    public String list(){        try {            //查看全部            List<Admin> list = adminService.getAll();            //保存到request            ActionContext.getContext().getContextMap().put("listAdmin", list);            return "list";        } catch (Exception e) {            return ERROR;        }    }}

拦截器UserCheckInterceptor.java

import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.ActionProxy;import com.opensymphony.xwork2.interceptor.AbstractInterceptor;public class UserCheckInterceptor extends AbstractInterceptor {    /**     * 拦截器业务处理方法     */    @Override    public String intercept(ActionInvocation invocation) throws Exception {        // 拿到当前执行的方法名,判断,只要当前方法名不是login就进行验证        // 获取ActionContext对象        ActionContext ac = invocation.getInvocationContext();        // 获取Action的代理对象        ActionProxy proxy = invocation.getProxy();        // 获取当前执行的方法名        String methodName = proxy.getMethod();        // 判断        if (!methodName.equals("login")) {            // 先获取当前登录的用户            Object obj = ac.getSession().get("userInfo");            if (obj == null) {                // 没有登录                return "input";            } else {                // 有登录                return invocation.invoke();            }        } else {            // 当前用户正在登录            return invocation.invoke();        }    }}

配置 struts.xml,注意其中拦截器的配置方式:

<package name="user" extends="struts-default">    <!-- 拦截器配置 -->        <interceptors>            <interceptor name="loginCheck" class="jp.yuudachi.interceptor.UserCheckInterceptor"></interceptor>            <interceptor-stack name="myStack">                <interceptor-ref name="defaultStack"></interceptor-ref>                <interceptor-ref name="loginCheck"></interceptor-ref>            </interceptor-stack>        </interceptors>        <!-- 执行拦截器 -->        <!-- 写法1 下面所有action都会执行mystack栈 -->        <default-interceptor-ref name="myStack"></default-interceptor-ref>        <!-- 全局配置 -->        <global-results>            <result name="error">/error.jsp</result>        </global-results>        <action name="user_*" class="jp.yuudachi.action.UserAction" method="{1}">            <!--写法2 只希望在一个action中执行mystack栈 -->            <!--             <interceptor-ref name="defaultStack"></interceptor-ref>            <interceptor-ref name="loginCheck"></interceptor-ref>             -->            <!-- 第三种写法 执行用户栈 -->            <!--             <interceptor-ref name="myStack></interceptor-ref>             -->            <!-- 登陆失败 -->            <result name="input">/login.jsp</result>            <!-- 登陆成功 -->            <result name="loginSuccess" type="redirectAction">user_list</result>            <!-- 列表展示 -->            <result name="list">/WEB-INF/list.jsp</result>        </action>    </package>
0 0