如何在struts2中为自定义拦截器添加excludeMethods参数列表 [英] how to add excludeMethods parameter list for custom interceptor in struts2
问题描述
如何在 struts.xml
文件中为我的自定义拦截器添加 excludeMethods
参数列表.workflow
和 validation
拦截器有这个参数,即 excludeMethods
通过女巫 workflow
拦截器不会为所描述的排除方法触发像这样:
How can I add excludeMethods
parameter list for my custom interceptor in struts.xml
file.
workflow
and validation
interceptor have this parameter i.e excludeMethods
through witch the workflow
interceptor will not fire for excluded methods as described like this:
<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
<%@ 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
<?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>
自定义拦截器
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();
}
}
动作类
package pack;
import com.opensymphony.xwork2.ActionSupport;
public class GoAction extends ActionSupport{
public String forGo2(){
return "success";
}
}
生成的输出没有任何错误.但是在控制台输出中,我正在查看#####Inside Interceptor#####
",这是我没想到的,因为我排除了 forGo2
方法的拦截器.在这种情况下,如何为任何给定方法排除此拦截器,例如 forGo2
.
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. How can I exclude this interceptor for any given method in this case like forGo2
.
推荐答案
为此有一个特定的基类:MethodFilterInterceptor
.来自文档:
There is a specific base class for this: 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 {
现在,不是覆盖 intercept
方法,而是覆盖 doIntercept
:
Now, instead of overriding the intercept
method, override doIntercept
:
@Override
public String doIntercept(ActionInvocation ai) throws Exception {
// TODO Auto-generated method stub
System.out.println("#####Inside Interceptor#####");
ai.invoke();
}
基类将自动处理 excludeMethods
并根据需要调用 doIntercept
.
The base class will handle excludeMethods
automatically and invoke doIntercept
as required.
这篇关于如何在struts2中为自定义拦截器添加excludeMethods参数列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!