struts2拦截器,拦截指定方法

Struts.xml配置文件

<?xml version="1.0"encoding="UTF-8"?>

<!DOCTYPE strutsPUBLIC

   "-//Apache Software Foundation//DTD StrutsConfiguration 2.0//EN"

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

<struts>

    <!-- 开发模式 -->

    <constant name="struts.devMode"value="true" />

    <!-- spring管理struts -->

    <constant name="struts.objectFactory"value="spring" />

 

    <package name="struts-shop" extends="struts-default">

 

       <interceptors>

           <!-- 先定义拦截器自己写的拦截器类 name随便写 class为全限定名 -->

           <interceptor name="filterInterceptor"class="com.bw.lanjieqi.FilterInterceptor"/>

             

           <!-- 加到自己设置的拦截器栈里边去 -->

           <interceptor-stack name="myStack">

<!--   name为上面interceptor标签里面定义的name-->

              <interceptor-ref name="filterInterceptor">

                  <!--拦截器黑白名单   includeMethods为拦截的方法 excludeMethods不拦截这俩可以单独存在 -->

                  <param name="includeMethods">toAdd</param>

                  <param name="excludeMethods">toUpdate</param>

                  <!--指定参数name的值   可以不写不需要就不写-->

                  <param name="name">FilterMethod</param>

              </interceptor-ref>

<!--   在自己配置的拦截器基础上,必须有struts2默认的拦截器,不然出错 -->

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

           </interceptor-stack>

 

       </interceptors>

       <!-- 应用拦截器不写拦截器不生效 -->

       <default-interceptor-ref name="myStack"/>

 

    </package>

 

    <!-- 将其他的struts2的配置文件加载到主配置文件struts.xml里 -->

    <include file="struts-action.xml" />

 

</struts>

 

 

Struts-action.xml配置文件

<?xml version="1.0"encoding="UTF-8"?>

<!DOCTYPE strutsPUBLIC

   "-//Apache Software Foundation//DTD StrutsConfiguration 2.0//EN"

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

<struts>

 

    <!-- extends继承struts.xml里的拦截器的配置 -->

    <package name="city"extends="struts-shop">

 

       <action name="city_*"class="com.bw.action.CityAction" method="{1}">

           <result name="list">/WEB-INF/view/list.jsp</result>

           <result name="toAdd">/WEB-INF/view/add.jsp</result>

           <result name="toUpdate">/WEB-INF/view/update.jsp</result>

           <result name="success"type="redirect">city_list</result>

 

          

       </action>

 

 

 

    </package>

 

 

 

</struts>

 

 

 

 

FilterInterceptor


package com.bw.lanjieqi;

 

 

import java.util.Date; 

 

import com.bw.action.CityAction;

import com.opensymphony.xwork2.ActionInvocation; 

importcom.opensymphony.xwork2.interceptor.MethodFilterInterceptor; 

 

publicclass FilterInterceptor extends MethodFilterInterceptor{ 

    //配置文件中指定的参数可以不用

    private String name; 

     

 

    public String getName() { 

        returnname; 

    } 

 

 

    publicvoid setName(Stringname) { 

        this.name = name; 

    } 

 

    @Override

    protected StringdoIntercept(ActionInvocation invocation) throws Exception { 

        // TODO Auto-generated method stub 

        //获取要拦截的action  如果不过过多的处理可以不写

        CityActionfa= (CityAction)invocation.getAction(); 

        System.out.println(name+"拦截器在Action执行前拦截"+new Date()); 

        String result=invocation.invoke(); 

        System.out.println(name+"拦截器在Action执行后拦截"+new Date()); 

        return result; 

    } 

 

}  


  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值