在Struts2中,我的Action中不管有execute方法,还有edit() list() add()等方法,我如果只想让拦截器拦截edit方法和add方法,请问可以吗?如果可以请告诉我一下怎么

在Struts2中,我的Action中不管有execute方法,还有edit()  list() add()等方法,我如果只想让拦截器拦截edit方法和add方法,请问可以吗?如果可以请告诉我一下怎么写。
我说的是拦截器拦截某些action方法。不是拦截所有的action方法,怎么实现。
 
1.继承类MethodFilterInterceptor(此类是类AbstractInterceptor的子类)

import java.util.Map;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;

/*
 *拦截指定方法 
 */
public class MyFilterInterceptor  extends MethodFilterInterceptor{
 private static final long serialVersionUID = 1L;
 private String name;
    public void setName(String name)
   {
        this.name = name;
    }
 @Override
 protected String doIntercept(ActionInvocation invocation) throws Exception {
  //取得请求相关的ActionContext实例 
  ActionContext ctx = invocation.getInvocationContext(); 
  Map session = ctx.getSession(); 
  //取出名为user的Session属性 
  String user = (String)session.get("user"); 
  //如果没有登陆,或者登陆所用的用户名不是scott,都返回重新登陆 
  if (user != null && user.equals("scott") ) 
  { 
  return invocation.invoke(); 
  } 
  //没有登陆,将服务器提示设置成一个HttpServletRequest属性 
  ctx.put("tip" , "您还没有登陆,请输入scott,tiger登陆系统"); 
  //直接返回login的逻辑视图 
  return Action.LOGIN; 
 }

}

2.struts.xml配置

<package name="site" extends="struts-default" namespace="/site">
 <interceptors> 
 <!-- 定义了一个名为authority的拦截器 --> 
  <interceptor name="authority" class="cn.zgcyx.filter.MyFilterInterceptor"/> <!--上面自定义的拦截器类-->
  <interceptor-stack name="myDefault">
   <interceptor-ref name="authority"> <!-- 引用拦截器  -->
    <param name="includeMethods">getALL,getPart,listUser</param> <!-- 设置需要拦截的方法,多个以逗号隔开 -->
   </interceptor-ref>
   <interceptor-ref name="defaultStack"></interceptor-ref>
  </interceptor-stack>
 </interceptors>
 <default-interceptor-ref name="myDefault"></default-interceptor-ref>

  <!-- 全局 -->
 <global-results> 
  <!-- 当返回login视图名时,转入/login.jsp页面 --> 
  <result name="login">/login.jsp</result> 
 </global-results>
<action name="site" class="siteServiceAction">

  <!--省略跳转-->

</action>

</package>
使用MethodFilterInterceptor 自定义方法过滤拦截器,然后:
在struts.xml配置文件中,找到interceptor-ref 标签,添加:
<param name="拦截器名称.includeMethods">edit,add</param>

自定义拦截器:

package net.hncu.interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class MyInterceptor3  extends MethodFilterInterceptor{
 //拦截器的名称
 private String interceptorName;
 
 public void setInterceptorName(String interceptorName) {
  this.interceptorName = interceptorName;
 }

 //实现拦截的方法
 public String intercept(ActionInvocation invocation) throws Exception {

  System.out.println(interceptorName + ":-----2拦截前操作-----");
  
  String result = invocation.invoke();
  
  System.out.println(interceptorName + ":------2拦截后操作------");
  
  return result;
 }
}

给一个struts.xml文件你参考:

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

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<!-- struts为配置文件根元素-->
<struts>
 <!-- Action必须放在指定的包名空间中-->
    <package name="struts2" extends="struts-default">
<interceptors>
   <interceptor name="myInter3" class="net.hncu.interceptor.MyInterceptor3">
         <param name="interceptorName">过滤拦截器</param>
   </interceptor>   
   <interceptor-stack name="myInterStack">
        <interceptor-ref name="myInter3">
             <param name="interceptorName">自定义过滤拦截器</param>
      </interceptor-ref> 
                 <interceptor-ref name="defaultStack"></interceptor-ref>
     </interceptor-stack>
</interceptors>
 
  <action name="login" class="net.hncu.struts2.action.LoginAction">
   <result name="success">/login_success.jsp</result>
   <result name="error">/login_failure.jsp</result>
   <result name="input">login.jsp</result>
   
<interceptor-ref name="myInterStack">
     <param name="myInter3.includeMethods">edit,add</param>  
    <!-- 用includeMethods指定被拦截的名称 -->
</interceptor-ref>
</action>
 </package>
 
 <!-- 指定资源文件baseName为messageResource -->
 <constant name="struts.custom.i18n.resources" value="messageResource"></constant>
</struts>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值