0

I have been trying to use interceptor with annotation in struts2 to handle my request and responses so that I can perform some pre and post actions.

But, I have used struts 2 with convention plugin initially which I can't really change. My framework also include spring in it.

But, now problem is whenever I have been trying to use interceptor as annotation in action it gives me following exception at the start of application.

SEVERE: Exception starting filter struts2
Unable to load configuration. - [unknown location]
    at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:483)
....
Caused by: Unable to load configuration. - [unknown location]
    at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:58)
.....
Caused by: Unable to find interceptor class referenced by ref-name mylogging - [unknown location]

My code structure is quite simple:

Action class seems like:

@InterceptorRefs({  
    @InterceptorRef("mylogging")  
}) 
    public class LoginAction implements ModelDriven{
.....
    @Action(value="/login",results={@Result(name="success",location="/jsp/successPage.jsp"),
                @Result(name="login",location="/jsp/userLogin.jsp")})
        public String execute() {
....

Struts.xml:

<struts>
    <constant name="struts.devMode" value="true" />
    <constant name="struts.ui.theme" value="simple" />
    <constant name="struts.enable.SlashesInActionNames" value="true" />
    <constant name="struts.mapper.alwaysSelectFullNamespace" value="false"/>
    <package  name="default"  namespace="" extends="struts-default">


     <interceptors>
            <interceptor name="mylogging" 
                class="com.lab.interceptor.LoggingInterceptor">
            </interceptor>
            <interceptor-stack name="loggingStack">
                <interceptor-ref name="mylogging" />
                <interceptor-ref name="defaultStack" />
            </interceptor-stack>
        </interceptors>
    </package>
</struts>

My deployment descriptor body (web.xml):

  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>
        org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
    </filter-class>
    <init-param>
            <param-name>actionPackages</param-name>
            <param-value>com.lab.actions</param-value>
        </init-param>
  </filter>


  <filter-mapping>

    <filter-name>struts2</filter-name>
    <url-pattern>*.action</url-pattern>
  </filter-mapping>
  <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/resources/config/SpringBeans.xml</param-value>
</context-param>

   <listener>
    <listener-class>
      org.springframework.web.context.ContextLoaderListener
    </listener-class>

  </listener>

Now hopefully it will help to figure out why it's throwing that exception, because i don't think I need to remove implementation of ModelDriven from my action.

Thanks in Advance

user2607985
  • 57
  • 3
  • 8
  • 1
    Where do you declare that your action lives in the `"default"` package? – Dave Newton Jul 22 '13 at 19:31
  • @DaveNewton I didn't get you but i think – user2607985 Jul 22 '13 at 20:01
  • 1
    That defines a package, but that is not where your actions will magically appear unless you specify that's where the action belongs. – Dave Newton Jul 22 '13 at 20:12
  • @DaveNewton http://stackoverflow.com/questions/16621970/what-is-the-default-package-name-for-struts-in-struts-xml tells that package name is independent of the package in which my actions exists so it's just a meaningful name that's it.... – user2607985 Jul 22 '13 at 20:23
  • 1
    ... Interceptors are defined *inside S2 (not Java) packages*. If your action is *in another S2 (not Java) package* it knows nothing about those interceptors (unless the action's S2 (not Java) package extends the S2 (not Java) package containing the interceptors, obviously). So. What S2 (not Java) package do you believe your action to be in? Hint: it is not "default", where your interceptor is defined. – Dave Newton Jul 22 '13 at 20:34
  • I have uploaded an image here http://postimg.org/image/yd7k1ddfp/ because I am not that much reputed to post an image as Just a new joiner. So red mark area is for interceptors and black marked area is for actions – user2607985 Jul 22 '13 at 20:46
  • What does that have to do with anything? I specifically differentiated between S2 packages and Java packages. – Dave Newton Jul 22 '13 at 22:35
  • Well I think I am not getting your point S2 Packages means struts 2 packages is it something different and Java Packages is the packages where I keep my all code.. I have shared the complete directory structure... – user2607985 Jul 22 '13 at 22:49
  • Do you see in the struts config where you use the word "package" as an XML configuration element? – Dave Newton Jul 22 '13 at 23:04
  • Yes obviously it's there – user2607985 Jul 23 '13 at 07:51

1 Answers1

2

By default the Convention plugin uses its own package convention-default which doesn't contain your package defined in struts.xml. To change that you have two options, both described in the docs [1]:

  • use @ParentPackage annotation
  • or define <constant name="struts.convention.default.parent.package" value="default"/> in struts.xml

[1] http://struts.apache.org/development/2.x/docs/convention-plugin.html#ConventionPlugin-ParentPackageannotation

Lukasz Lenart
  • 987
  • 7
  • 14