应该为自定义Interceptor类和包使用什么命名约定,以避免Struts2中的struts.xml

Nam*_*pal 5 java jsp struts2

我有兴趣知道是否有任何约定在struts2中命名自定义拦截器,以便自动检测拦截器,就像动作类一样.

我正在使用" struts2-convention-plugin-2.3.24.1.jar ".

包结构如下

ProtienTracker
    >Java Resources
        >src
            >com.nagarro.actions
                >HelloAction.java
            >com.nagarro.interceptors
                >custInterceptor.java
    >WebContent
        >META_INF
        >WEB_INF
            >content
                >hello.jsp
            >lib
        >web.xml
Run Code Online (Sandbox Code Playgroud)

代码在没有" struts.xml "且没有" custInterceptor "的情况下运行完美.struts2-convention-plugin自动检测到该操作.

只要我附上拦截器

@org.apache.struts2.convention.annotation.Action(value="hello",
    interceptorRefs=@InterceptorRef("custInterceptor"))
Run Code Online (Sandbox Code Playgroud)

我收到错误,如下所示.

文件如下

为hello.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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>Hello</title>
</head>
<body>
    <h1><s:property value="greeting"/></h1>
    <p>hi</p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

HelloAction.java

package com.nagarro.actions;
import com.opensymphony.xwork2.Action;

import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Result;
public class HelloAction implements Action {

    private String greeting="ab";

    @Override
    @org.apache.struts2.convention.annotation.Action(value="hello",
    interceptorRefs=@InterceptorRef("custInterceptor"))
    public String execute() throws Exception {
        setGreeting("Hello Structs 2");
        return "success";
    }

    public String getGreeting() {
        return greeting;
    }

    public void setGreeting(String greeting) {
        this.greeting = greeting;
    }

}
Run Code Online (Sandbox Code Playgroud)

custInterceptor.java

package com.nagarro.interceptors;

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

public class custInterceptor implements Interceptor{

    private static final long serialVersionUID = 1L;

    @Override
    public void destroy() {
        System.out.println("custInterceptor destroy() is called...");

    }

    @Override
    public void init() {
        System.out.println("custInterceptor init() is called...");

    }

    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
        System.out.println("custInterceptor intercept() is called...");
        System.out.println(invocation.getAction().getClass().getName());
        return invocation.invoke();
    }

}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

Caused by: Unable to find interceptor class referenced by ref-name custInterceptor - [unknown location]
        at com.opensymphony.xwork2.config.providers.InterceptorBuilder.constructInterceptorReference(InterceptorBuilder.java:63)
        at org.apache.struts2.convention.DefaultInterceptorMapBuilder.buildInterceptorList(DefaultInterceptorMapBuilder.java:95)
        at org.apache.struts2.convention.DefaultInterceptorMapBuilder.build(DefaultInterceptorMapBuilder.java:86)
        at org.apache.struts2.convention.DefaultInterceptorMapBuilder.build(DefaultInterceptorMapBuilder.java:70)
        at org.apache.struts2.convention.PackageBasedActionConfigBuilder.createActionConfig(PackageBasedActionConfigBuilder.java:947)
        at org.apache.struts2.convention.PackageBasedActionConfigBuilder.buildConfiguration(PackageBasedActionConfigBuilder.java:734)
        at org.apache.struts2.convention.PackageBasedActionConfigBuilder.buildActionConfigs(PackageBasedActionConfigBuilder.java:355)
        at org.apache.struts2.convention.ClasspathPackageProvider.loadPackages(ClasspathPackageProvider.java:53)
        at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:274)
        at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:67)
        ... 17 more
Run Code Online (Sandbox Code Playgroud)

Ali*_*ahi 1

正如在使用 struts2 注释定义拦截器中提到的。

如果您使用自定义拦截器,则无法消除对 struts.xml 的需求。

要在 xml 中定义新的拦截器,您需要在 struts.xml 文件中定义它

<struts>
    ......
 <package name="my-default" extends="struts-default" />

   <!-- Define the intercepor class and give it a name-->
    <interceptors>
       <interceptor name="custInterceptor"  class=com.nagarro.interceptors.custInterceptor" />
    </interceptors>

   <!-- Add it to a package. for example I add 
              the interceptor at top of struts default stack-->
   <interceptor-stack name="myDefaultStack">
      <interceptor-ref name="custInterceptor"/>
      <interceptor-ref name="defaultStack"/>
   </interceptor-stack>    

  <!-- Use the interceptor stack in your action with @InterceptorRef or set it as default -->
  <default-interceptor-ref name="myDefaultStack" />

 </package>
</struts>
Run Code Online (Sandbox Code Playgroud)

您可以将 struts-default.xml https://struts.apache.org/docs/struts-defaultxml.html视为有关如何定义和使用拦截器的一个很好的示例。