zoukankan      html  css  js  c++  java
  • Struts2 拦截器

    编写一个拦截ScheduleAction的拦截器

    package com.manager.interceptor;
    
    import java.util.Date;
    
    import com.manager.action.ScheduleAction;
    import com.opensymphony.xwork2.ActionInvocation;
    import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
    import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
    
    public class ScheduleInterceptor extends MethodFilterInterceptor {
    
    	// 为该拦截器取一个名称
    	private String name;
    
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	@Override
    	protected String doIntercept(ActionInvocation arg0) throws Exception {
    		// TODO Auto-generated method stub
    		// 取得被拦截的Action的实例
    		ScheduleAction scheduleAction = (ScheduleAction) arg0.getAction();
    
    		// 打印执行开始的实现
    		System.out.println(name + "拦截器的动作----------"
    				+ "开始执行ScheduleAction的时间为:" + new Date());
    
    		// 取得开始执行Action的时间
    		long start = System.currentTimeMillis();
    
    		// 执行该拦截器的后一个拦截器
    		// 如果该拦截器后没有其他拦截器,则直接执行拦截器中includeMethods对应的方法
    
    		String result = arg0.invoke();
    
    		return result;
    	}
    
    }
    

    struts.xml中配置该拦截器

            <interceptors>
                <!-- 配置ScheduleInterceptor -->
                <interceptor name="ScheduleInterceptor" class="com.manager.interceptor.ScheduleInterceptor">
                    <!-- 为拦截器指定参数值 -->
                    <param name="name">Schedule</param>
                </interceptor>
            </interceptors>

    为action应用此拦截器

    <action name="schedule" class="schedule">
                <result name="success"></result>
                <!-- 配置系统的默认拦截器 -->
                <interceptor-ref name="defaultStack"></interceptor-ref>
                <!-- 应用自定义的Schedule拦截器 -->
                <interceptor-ref name="ScheduleInterceptor">
                    <param name="name">该明后的拦截器</param>
                    <!-- 需要拦截的方法 list为Action中的方法名-->
                    <param name="includeMethods">list</param>
                </interceptor-ref>
            </action>

    配置拦截器栈

            <interceptors>
                <!-- 配置ScheduleInterceptor -->
                <interceptor name="ScheduleInterceptor" class="com.manager.interceptor.ScheduleInterceptor">
                    <!-- 为拦截器指定参数值 -->
                    <param name="name">Schedule</param>
                </interceptor>
                <!-- 配置一个名为myInterceptor的拦截器栈 -->
                <interceptor-stack name="myInterceptor">
                    <!-- 将ScheduleInterceptor拦截器放入该栈中 -->
                    <interceptor-ref name="ScheduleInterceptor"></interceptor-ref>
                    <!-- 可以引用多个  -->
                </interceptor-stack>    
            </interceptors>


    此时只需要给Action配置该拦截器栈 该Action就能被栈中所有的拦截器拦截了

            <action name="schedule" class="schedule">
                <result name="success"></result>
                <!-- 配置系统的默认拦截器 -->
                <interceptor-ref name="defaultStack"></interceptor-ref>
                <!-- 应用myInterceptor拦截器栈 -->
                <interceptor-ref name="myInterceptor">
                    <param name="includeMethods">list</param>
                </interceptor-ref>
            </action>
  • 相关阅读:
    二十二、Linux 进程与信号---进程创建
    二十一、Linux 进程与信号---进程查看和进程状态、进程调度和进程状态变化、进程标识
    二十一、Linux 进程与信号---进程资源限制
    二十、Linux 进程与信号---非局部跳转
    AOSP android 源码下载
    十九、Linux 进程与信号---环境表
    十八、Linux 进程与信号---进程介绍
    09 scroll系列_简单的封装动画函数(变速(缓动)的动画函数_getComputedStyle函数_复杂的动画函数封装_颜色相关_案例_client系列
    A 第二课 栈_队列_堆
    pycharm 的快捷键
  • 原文地址:https://www.cnblogs.com/mingf123/p/3733882.html
Copyright © 2011-2022 走看看