gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 05:17:08 25 4
gpt4 key购买 nike

如何在 struts.xml 文件中为自定义拦截器添加 excludeMethods 参数列表。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>

这个我知道。我想知道的是如何为我的自定义拦截器做同样的事情。我尝试过但失败了。这是我的代码:

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();
}

}

Action 类

package pack;

import com.opensymphony.xwork2.ActionSupport;

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

生成的输出没有任何错误。但在控制台输出中,我看到的是“#####Inside Interceptor#####”,这是我没有预料到的,因为我排除了 forGo2 方法的拦截器。在这种情况下,如何为任何给定方法排除此拦截器,例如 forGo2

最佳答案

为此有一个特定的基类:MethodFilterInterceptor 。来自文档:

An abstract Interceptor that is applied to selectively according to specified included/excluded method lists.

要使用,首先在拦截器中扩展它:

public class MyInterceptor2 extends MethodFilterInterceptor {

现在,不要重写 intercept 方法,而是重写 doIntercept:

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

基类将自动处理excludeMethods并根据需要调用doIntercept

关于java - 如何在struts2中为自定义拦截器添加exportMethods参数列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26828861/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com