简体   繁体   English

如何在struts2中为自定义拦截器添加excludeMethods参数列表

[英]how to add excludeMethods parameter list for custom interceptor in struts2

How can I add excludeMethods parameter list for my custom interceptor in struts.xml file. 如何在struts.xml文件中为自定义拦截器添加excludeMethods参数列表。 workflow and validation interceptor have this parameter ie excludeMethods through witch the workflow interceptor will not fire for excluded methods as described like this: workflowvalidation拦截器有这个参数即excludeMethods通过巫的workflow拦截器将不会触发如这样描述排除方法:

<action name="action" class="abc.ActionClass">
<interceptor-ref name="defaultStack">
<param name="workflow.excludeMethods">doSomething</param>
</interceptor-ref>
<result>Success.jsp</result>
</action> 

This I know. 这我知道。 What I want to know is how can I do the same for my custom interceptor. 我想知道的是如何对自定义拦截器执行相同的操作。 I tried but failed. 我尝试了但是失败了。 Here is my code: 这是我的代码:

index.jsp index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/struts-tags" prefix="s" %>    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<s:url action="go2"  method="forGo2" var="v_go2"/>
<a href="<s:property value='#v_go2'/>">HIT to check if excludeMethods parameter working or NOT.</a>
</body>
</html>

struts.xml struts.xml

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


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

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

<interceptors>

<interceptor name="cust_intrcptr" class="pack.MyInterceptor2">
<param name="excludeMethods">forGo2</param> <!-- parameter for excluded method -->
</interceptor>

<interceptor-stack name="mystack2">
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="cust_intrcptr"/>
</interceptor-stack>

</interceptors>

<action name="go2" class="pack.GoAction" method="forGo2">
<interceptor-ref name="mystack2"/>
<result name="success">/welcome2.jsp</result>
</action>
</package>
</struts>   

custom-interceptor 定制拦截器

package pack;

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

public class MyInterceptor2 implements Interceptor{

    @Override
    public void destroy() {
        // TODO Auto-generated method stub

    }

    @Override
    public void init() {
        // TODO Auto-generated method stub

    }

    @Override
    public String intercept(ActionInvocation ai) throws Exception {
        // TODO Auto-generated method stub
      System.out.println("#####Inside Interceptor#####");
      ai.invoke();
    }

}

Action Class 动作班

package pack;

import com.opensymphony.xwork2.ActionSupport;

public class GoAction extends ActionSupport{
    public String forGo2(){
        return "success";
    }
}

Output generated without any errors. 生成的输出没有任何错误。 But in console output I am viewing " #####Inside Interceptor##### " that I did not expected because I excluded the interceptor for forGo2 method. 但是在控制台输出中,我正在查看我没有想到的“ #####Inside Interceptor##### ”,因为我排除了forGo2方法的拦截器。 How can I exclude this interceptor for any given method in this case like forGo2 . 在这种情况下,如何为任何给定的方法(例如forGo2排除此拦截器。

There is a specific base class for this: MethodFilterInterceptor . 为此有一个特定的基类: MethodFilterInterceptor From the Documentation: 从文档中:

An abstract Interceptor that is applied to selectively according to specified included/excluded method lists. 根据指定的包含/排除方法列表有选择地应用的抽象拦截器。

To use, first extend it in your interceptor: 要使用它,首先在您的拦截器中扩展它:

public class MyInterceptor2 extends MethodFilterInterceptor {

Now, instead of overriding the intercept method, override doIntercept : 现在,取代doIntercept ,而不是重写intercept方法:

@Override
public String doIntercept(ActionInvocation ai) throws Exception {
    // TODO Auto-generated method stub
  System.out.println("#####Inside Interceptor#####");
  ai.invoke();
}

The base class will handle excludeMethods automatically and invoke doIntercept as required. 基类将自动处理excludeMethods并根据需要调用doIntercept

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM