What naming convention should be used for user class and Interceptor package to avoid struts.xml in Struts2

I am interested to know if there is any convention for designating a custom interceptor in struts2 to automatically detect interceptors, for example for action classes.

I am using struts2-convention-plugin-2.3.24.1.jar .

The package structure is as follows

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 

The code works flawlessly without struts.xml "and without custInterceptor ". The action is automatically determined by the struts2 plugin.

As soon as I attach the interceptor with

 @org.apache.struts2.convention.annotation.Action(value="hello", interceptorRefs=@InterceptorRef ("custInterceptor")) 

i get the error as below.

Files follow

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> 

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

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

I get the following error:

 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 
+5
source share
1 answer

As mentioned in define interceptors with struts2 annotations .

you cannot eliminate the need for struts.xml if you use custom interceptors.

To define a new interceptor in xml, you need to define it in struts.xml files.

 <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> 

You can see struts-default.xml https://struts.apache.org/docs/struts-defaultxml.html as a good example of how interceptors are defined and used.

+1
source

Source: https://habr.com/ru/post/1246061/


All Articles