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>
  • 相关阅读:
    @JsonFormat和@DateTimeFormat 实践测试
    spring jpa CrudRepository save 新建数据没有返回id
    多线程处理pdf附件转换
    contentsize ,ios 7和 ios7之前的 有点差别,
    区别,
    裁切图片,
    transform,
    简洁代码,
    这个系统,流程,入口,业务逻辑,
    pop,pop,如果break,会pop两次,
  • 原文地址:https://www.cnblogs.com/mingf123/p/3733882.html
Copyright © 2011-2022 走看看