struts2学习之第四天

Struts2拦截器概述

1.      struts2是框架,封装了很多功能,struts2里面封装的功能都是在拦截器里面

2.      struts2里面封装了很多功能,有很多拦截器,每次执行默认的一部分拦截器

3.      struts2里面默认拦截器的位置

struts2-core-2.3.32.jar内的struts-default.xml配置文件内

<interceptor-stackname=" ">

    <interceptor-refname=" "/>

    <interceptor-refname=" "/>

</interceptor-stack>

4.      拦截器在什么时候执行?

(1)    在action对象创建之后,action里面的方法执行之前

拦截器底层原理

1.      拦截器底层使用两个原理

(1)    aop思想

面向切面(方面)编程。可以简单理解为有基本功能想要扩展,不通过源代码进行

(2)    责任链模式

a.      Java设计模式的一种

b.      和过滤链相似

c.      过滤链:一个请求可以由多个过滤器进行过滤,每个过滤器只有做放行操作才可以到下一个过滤器

d.      责任链:要执行多个操作,如有添加修改删除三个操作,首先执行添加之后做一个类似于放行的操作,然后修改,之后做一个类似于放行的操作,然后删除

2.      aop思想和责任链模式如何应用到拦截器中

(1)    文字描述:

a.      拦截器在action对象创建之后,action中的方法执行之前执行,执行过程使用aop思想,在action中并没有直接调用拦截器方法,而是使用配置文件进行操作

b.      在执行很多拦截器的时候,使用责任链模式

重要概念

1.      过滤器和拦截器的区别

 (1)    过滤器:过滤器可以理论上可以过滤任意内容,如html jsp servlet等

 (2)    拦截器:拦截器只可以拦截action

2.      Servlet和action的区别

 (1)    servlet默认第一次访问创建,单实例对象,创建一次

 (2)    action每次访问时候创建,多实例对象,创建多次

自定义拦截器

1.      在struts2里面有很多拦截器,拦截器中封装了很多功能,但是在实际应用开发中,拦截器中没有满足开发人员需求的功能,这个时候需要自定义拦截器

2.      拦截器结构

(1)    查看源代码观察结构(以ModelDriven为例)

<interceptor name="modelDriven" class="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor"/>

a.      继承类

public class ModelDrivenInterceptor extends AbstractInterceptor

b.      AbstractIntercepto类实现了接口,有三个方法

public abstract class AbstractInterceptor implements Interceptor {

    /**
     * Does nothing
     */
    public void init() {
    }
    
    /**
     * Does nothing
     */
    public void destroy() {
    }


    /**
     * Override to handle interception
     */
    public abstract String intercept(ActionInvocation invocation) throws Exception;
}

(2)    开发中,建议使用另一种方式

写一个类继承 MethodFilterInterceptor类(顾名思义,方法过滤拦截器)实现   

让action里面某个方法不进行拦截

(3)    让拦截器和action中有关系

不是在action中调用拦截器方法,而是通过配置文件方式建立关系

(例如)自定义登陆拦截器

1、  需求:在非登录状态下点击相关连接时返回登陆界面

2、  写拦截器

public class LoginInterceptor extends MethodFilterInterceptor {

    @Override
    protected String doIntercept(ActionInvocation invocation) throws Exception {
        // TODO Auto-generated method stub
        HttpServletRequest request = ServletActionContext.getRequest();
        User user = (User) request.getSession().getAttribute("user");
        if (user != null) {
            // 放行
            return invocation.invoke();
        } else {
            // 不是登陆状态 返回登陆页面
            return "exit";
        }
    }

}
3、配置

  <package name="myProject" extends="struts-default" namespace="/">
    <!-- 1声明拦截器 -->
    <interceptors>
      <interceptor name="loginIntercept" class="com.bpf.interceptor.LoginInterceptor"></interceptor>    
    </interceptors>
    <action name="customer_*" class="com.bpf.action.CustomerService" method="{1}">
    
      <!-- 2使用拦截器 -->
      <interceptor-ref name="loginIntercept">
        <!-- 配置某些方法不进行拦截
             name属性值:excludeMethods
             值:不拦截的方法名
         -->
        <param name="excludeMethods"><!-- 方法名1,方法名2 --></param>
      </interceptor-ref>
      <!-- 3手动开启默认拦截器 -->
      <interceptor-ref name="defaultStack"></interceptor-ref>
      
      <result name="addSuccess">/addCustomer.jsp</result>
      <result name="addFail">/registerFail.html</result>
      <result name="showSuccess">/showCustomer.jsp</result>
      <result name="showFail">/registerFail.html</result>
      <result name="exit">/login.jsp</result>
    </action>
  </package>



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值