Note that there are some explanatory texts on larger screens.

plurals
  1. POstruts2 adding interceptors in struts.xml for all action class
    text
    copied!<p>I've used the Struts 2 framework and I have created a web application which has a Login Page. I have three different <code>Action</code> classes named <code>Action1</code>, <code>Action2</code>, <code>Action3</code>, and different views for JSP pages which are rendered by running some business logic in the <code>Action</code> classes.</p> <p>Now, I want to check whether a user has logged in before the <code>Action</code> class carries out processing. So, I created an interceptor below that works fine.</p> <pre><code>public String intercept(ActionInvocation invocation) throws Exception { HttpServletRequest request = ServletActionContext.getRequest(); HttpServletResponse response = ServletActionContext.getResponse(); HttpSession session = request.getSession(); if(session.isNew()) { response.sendRedirect("Login.action"); } System.out.println("Interceptor Fired"); String result = invocation.invoke(); return result; } </code></pre> <p>What I want to be in <code>struts.xml</code> is instead of adding an interceptor for all the actions like the one below</p> <pre><code>&lt;interceptor-ref name="newStack"/&gt; </code></pre> <p>My <code>struts.xml</code> file has</p> <pre><code>&lt;package name="default" extends="struts-default"&gt; &lt;interceptors&gt; &lt;interceptor name="printMsgInterceptor" class="LoginInterceptor"&gt;&lt;/interceptor&gt; &lt;interceptor-stack name="newStack"&gt; &lt;interceptor-ref name="printMsgInterceptor"/&gt; &lt;interceptor-ref name="defaultStack" /&gt; &lt;/interceptor-stack&gt; &lt;/interceptors&gt; &lt;action name="actone" class="Action1"&gt; &lt;result name="success"&gt;/success.jsp&lt;/result&gt; &lt;interceptor-ref name="newStack"/&gt; &lt;/action&gt; &lt;action name="acttwo" class="Action2"&gt; &lt;result name="success"&gt;/success.jsp&lt;/result&gt; &lt;interceptor-ref name="newStack"/&gt; &lt;/action&gt; &lt;action name="actthree" class="Action3"&gt; &lt;result name="success"&gt;/success.jsp&lt;/result&gt; &lt;interceptor-ref name="newStack"/&gt; &lt;/action&gt; &lt;/package&gt; </code></pre> <p>For every action I want to have some definition written in <code>struts.xml</code> which runs the interceptor rather than manually adding </p> <pre><code>&lt;interceptor-ref name="newStack"/&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload