how to use interceptor defined in different packages in struts 2?
Asked Answered
M

2

7

I have defined an interceptor as follows:

<package name="default" extends="struts-default" >
  <interceptors>
       <interceptor-stack name="myStack">
          <interceptor-ref name="timer"/>
          <interceptor-ref name="logger"/>
        <interceptor-ref name="defaultStack"/>
       </interceptor-stack>
  </interceptors>

 <default-interceptor-ref name="myStack"/>

</package>

And then use the myStack in another namespace:

<package name="posts" namespace="/posts" extends="struts-default,json-default">

        <action  name="question/ask" class="someclass.QuestionAction">
            <interceptor-ref name="myStack"></interceptor-ref>
            <result name="success">/WEB-INF/jsp/post_question.jsp</result>
            <result name="input">/WEB-INF/jsp/post_question.jsp</result>
        </action>

    </package>

This did not work because in the package posts, it could not find the interceptor stack named myStack. How can I solve this problem?

Mashburn answered 27/1, 2013 at 2:49 Comment(0)
R
8

Having package "posts" extending "default" would solve the issue.

Rummage answered 27/1, 2013 at 5:28 Comment(0)
S
2

There is two ways is there to include the interceptors in struts.xml

First:

1)If you write any interceptors in other xml file & you want to use that interceptors in struts.xml file means, you should include that file in struts.xml

Eg: Consider other.xml file is file.xml & you want to include into the struts.xml so,

in struts.xml you have to write

<struts> 
     <include file="file.xml"></include>
      <package name="posts" namespace="/posts" extends="struts-default,json-default">
         <action  name="question/ask" class="someclass.QuestionAction">
                <interceptor-ref name="myStack"></interceptor-ref>
                <result name="success">/WEB-INF/jsp/post_question.jsp</result>
                <result name="input">/WEB-INF/jsp/post_question.jsp</result>
            </action>
      </package>

</struts>  

Second Way: You Should Include the interceptors within struts.xml and refer the name in your action class then it will work correctly like this.

<package name="default" extends="struts-default">
   <interceptors>
        <interceptor name="timer" class=".."/>
        <interceptor name="logger" class=".."/>
        <interceptor-stack name="myStack">
           <interceptor-ref name="timer"/>
           <interceptor-ref name="logger"/>
        </interceptor-stack>
    </interceptors>

<action name="login"
     class="tutuorial.Login">
         <interceptor-ref name="myStack"/>
         <result name="input">login.jsp</result>
         <result name="success"
             type="redirect-action">/secure/home</result>
</action>
</package>

Also i am giving two links, refer this links one of the eg i referred from these links only. Full Interceptors Concept . Basic Interceptors

Soaring answered 27/1, 2013 at 8:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.