5

I am interested to know if there is any convention for naming custom interceptor in struts2 for the auto detection of interceptors like there are for the action classes.

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

The Package structure is as follow

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 runs perfect without "struts.xml" and without "custInterceptor". The action is automatically detected by the struts2-convention-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 shown below.

The files are as 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
Naman Kheterpal
  • 1,810
  • 1
  • 9
  • 16
  • Read the [docs](https://struts.apache.org/docs/convention-plugin.html). – Aleksandr M Mar 30 '16 at 10:04
  • 2
    *If you get errors like "Unable to find interceptor class referenced by ref-name XYZ". This means that the package where Convention is placing your actions, does not extend the package where the interceptor is defined. To fix this problem either 1)Use @ParentPackage annotation(or struts.convention.default.parent.package) passing the name of the package that defines the interceptor, or 2) Create a package in XML that extends the package that defines the interceptor, and use @ParentPackage(or struts.convention.default.parent.package) to point to it.* – Aleksandr M Mar 30 '16 at 10:04
  • can you please tell what code statement should I add in my "custInterceptor.java" class ? Or any link example for the code in Interceptor class? – Naman Kheterpal Mar 30 '16 at 10:11
  • 1
    You can implement your own `ConfigurationProvider` to discover interceptors and add them to the global config http://struts.apache.org/docs/configuration-provider-configuration.html – Lukasz Lenart Mar 30 '16 at 14:53

1 Answers1

1

As mentioned in define interceptors with struts2 annotations.

you cannot eliminate a need of struts.xml if you are using 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 sample for how interceptors are defined and used.

Community
  • 1
  • 1
Alireza Fattahi
  • 42,517
  • 14
  • 123
  • 173
  • This is only works for xml based configuration. But if you use some configuration, it will work with convention plugin. – Roman C Mar 31 '16 at 11:23