struts2[4.2]拦截器----拦截器配置介绍

接着上次的~

在此,我先引入上次的一张图片,帮助我们认识程序走的流程~

1.步骤

1.1注册拦截器

1.2注册拦截器栈(当自己没定义拦截器的时候,引用默认的20个拦截器)

1.3指定默认的拦截器

<package name="inter" namespace="/" extends="struts-default" >
	<!-- 1.注册拦截器 -->
	<interceptors>
		<interceptor name="myInter3" class="com.aisino.a_interceptor.MyInterceptor3"></interceptor>
	<!-- 2.注册拦截器栈 -->
		<interceptor-stack name="myStack">
			<!-- 自定义拦截器引入(建议放在默认拦截器之前) -->
			<interceptor-ref name="myInter3"></interceptor-ref>
			<!-- 引用默认的拦截器栈(20) -->
			<interceptor-ref name="defaultStack"></interceptor-ref>
		</interceptor-stack>
	</interceptors>
	<!-- 3.指定默认拦截器栈 -->
  	   	    <!-- 
	<default-interceptor-ref name="myStack"></default-interceptor-ref>
 		    -->
	
		<action name="Demo1Action" class="com.aisino.a_interceptor.Demo1Action" method="execute" >
			<!-- 在action中可单独为Demo1Action指定走那个拦截器(栈) -->
			<interceptor-ref name="myStack"></interceptor-ref>
			<result name="success" type="dispatcher" >/index.jsp</result>
		</action>
	</package>

2.当我们访问Demo1Action

package com.aisino.a_interceptor;

import com.opensymphony.xwork2.ActionSupport;

public class Demo1Action extends ActionSupport{

	@Override
	public String execute() throws Exception {
		System.out.println("Demo1Action!");
		return SUCCESS;
	}

}

的时候程序走的流程:

2.1在主配置struts.xml中走Demo1Action,Demo1Action便开始走我们定义的拦截器(栈)mystack,

<action name="Demo1Action" class="com.aisino.a_interceptor.Demo1Action" method="execute" >
			<!-- 在action中可单独为Demo1Action指定走那个拦截器(栈) -->
			<interceptor-ref name="myStack"></interceptor-ref>

2.2首先走我们自己定义的拦截器MyInterceptor3(痕迹我们有在控制台输出),看最上方的图,首先进行前处理,走Interceptor1~3,那么拦截器的前处理就会在控制台中输出;接着走到action,我们就访问了一个叫Demo1Action的action,她也会在控制台输出;接下来就会走后处理,在控制台打印输出,

public class MyInterceptor3 extends MethodFilterInterceptor {

	@Override
	protected String doIntercept(ActionInvocation invocation) throws Exception {

		//invoke()之前----前处理
		System.out.println("MyMyInterceptor3的前处理");
		
		//放行
		invocation.invoke();    //放行,就调用invoke
		
		//invoke()之后----后处理
		System.out.println("MyMyInterceptor3的后处理");
		
		return null;            //不放行,直接return一个结果
	}


}

接着走默认的拦截器(没有打印输出!)

2.3当访问http://localhost:8080/struts2_day04Test/Demo1Action的时候,控制台就会通过拦截器访问Demo1Action:

信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
MyMyInterceptor3的前处理
Demo1Action!
MyMyInterceptor3的后处理

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值