Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

jsp

struts2

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
like image 641
Naman Kheterpal Avatar asked Nov 09 '22 17:11

Naman Kheterpal


1 Answers

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.

like image 138
Alireza Fattahi Avatar answered Nov 14 '22 23:11

Alireza Fattahi