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

I defined the 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 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 message package he could not find the interceptor stack named myStack . How can I solve this problem?

+7
source share
2 answers

Having the "posts" package extending "default" will solve the problem.

+8
source

There are two ways to enable interceptors in struts.xml

 First: 

1) If you write any interceptors in another XML file, and you want to use these interceptors in the struts.xml file, you must include this file in struts.xml

For example: consider other.xml file is file.xml and you want to include in struts.xml like this

in struts.xml you need 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 have to include interceptors in struts.xml and specify a 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> 

I also give two links, refer to these links, one of which, for example, I refer only to these links. The concept of full interceptors . Base interceptors

+2
source

All Articles