Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use interceptor defined in different packages in struts 2?

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?

like image 834
ipkiss Avatar asked Jan 27 '13 02:01

ipkiss


2 Answers

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

like image 162
Quaternion Avatar answered Nov 15 '22 11:11

Quaternion


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

like image 42
Raki Avatar answered Nov 15 '22 10:11

Raki