struts2的拦截器Interceptor

来源:互联网 发布:cf网络异常23 2 编辑:程序博客网 时间:2024/05/17 09:14

package com.lanou.controller.interceptors;


import com.opensymphony.xwork2.ActionInvocation;

import com.opensymphony.xwork2.interceptor.Interceptor;


public class TestInterceptorimplements Interceptor{


@Override

public void destroy(){

}


@Override

public void init(){

}

//ActionInvocation可以帮助获得action对象

@Override

publicString intercept(ActionInvocationinvoc) throws Exception{

System.out.println("第二个开始拦截");

            String str=invoc.invokeActionOnly();

           

            System.out.println(str);

            System.out.println("第二个拦截器内容是:"+str);

return"success";

}

struts.xml

<?xmlversion="1.0"encoding="UTF-8"?>

<!DOCTYPE strutsPUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

"http://struts.apache.org/dtds/struts-2.3.dtd">


<struts>

<!-- -->

<constantname="struts.enable.DynamicMethodInvocation"value="false"/>


<constantname="struts.devMode"value="true"/>


<packagename="default"namespace="/"extends="json-default">

<!-- 在学习servlet的时候,filter是通过url-pattern关联到一起的 -->

<!--filetr是通过配置文件配置进来的 -->

<!--拦截器interceptor也是通过配置文件进入系统的 ,执行过程123,321,先进入web.xml文件,在进入action,再进拦截器 -->

<interceptors>

<!-- 自定义拦截器实现Interceptor接口或实现AbstractInterceptor 类 -->

<interceptorname="test"

class="com.lanou.controller.interceptors.TestInterceptor"></interceptor>

<interceptorname="second"

class="com.lanou.controller.interceptors.SecondInterceptor"></interceptor>

<interceptor-stackname="yufei">

<interceptor-refname="second"></interceptor-ref>


  <interceptor-ref name="defaultStack"></interceptor-ref

<interceptor-refname="test"></interceptor-ref>

</interceptor-stack>

</interceptors>

<actionname="test"class="com.lanou.controller.actions.TestAction"

method="test">

<!-- -interceptor的配置过程在action标签里面 ,在没有action -->

<interceptor-refname="yufei"></interceptor-ref>

<resultname="success">

/test.html

</result>

</action>

</package>


<includefile="example.xml"/>




</struts>


}

原创粉丝点击