Adding interceptors in struts.xml for all Action classes
Asked Answered
S

2

7

I've used the Struts 2 framework and I have created a web application which has a Login Page. I have three different Action classes named Action1, Action2, Action3, and different views for JSP pages which are rendered by running some business logic in the Action classes.

Now, I want to check whether a user has logged in before the Action class carries out processing. So,

I created an interceptor below that works fine:

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;
}

What I want to be in struts.xml is instead of adding an interceptor for all the actions like the one below

<interceptor-ref name="newStack"/>

My struts.xml file has:

<package name="default" extends="struts-default">       
   <interceptors>   
    <interceptor name="printMsgInterceptor" class="LoginInterceptor"></interceptor>
         <interceptor-stack name="newStack">
        <interceptor-ref name="printMsgInterceptor"/>
        <interceptor-ref name="defaultStack" />
         </interceptor-stack>
     </interceptors>

    <action name="actone" class="Action1">
      <result name="success">/success.jsp</result>
      <interceptor-ref name="newStack"/>
    </action>
        <action name="acttwo" class="Action2">
      <result name="success">/success.jsp</result>
      <interceptor-ref name="newStack"/>
    </action>
         <action name="actthree" class="Action3">
      <result name="success">/success.jsp</result>
      <interceptor-ref name="newStack"/>
    </action>
    </package>

For every action I want to have some definition written in struts.xml which runs the interceptor rather than manually adding

<interceptor-ref name="newStack"/> 
Skidmore answered 5/6, 2013 at 12:39 Comment(0)
B
13
<interceptor name="test" class="Full path for LoginInterceptor" />

    <interceptor-stack name="testStack">  
         <interceptor-ref name="test"/>
         <interceptor-ref name="defaultStack"/> //here you are including default stack
    </interceptor-stack> 

</interceptors>  
<default-interceptor-ref name="testStack"></default-interceptor-ref>

Now testStack will execute for every request

Benzvi answered 5/6, 2013 at 12:43 Comment(3)
Thanks for Reply It works gr8 I dont want this interceptor for only one page that is Login Page because it is resulting in redirect loop.Is there a way to avoid interceptor for one particular ActionSkidmore
<action name="actionname" class="class"> <interceptor-ref name="default"/> .... </action>Benzvi
Alternatively you can define the interceptor stack for a specific package and every package that extends it will use its default stack. Other actions that should not be intercepted (such as Login) can be defined in other packages that doesn't extend the one where you defined the interceptor default stackSeppuku
X
5

Use

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

If you not putting interceptor-ref manually for each action you could use the default-interceptor-ref to intercept all actions that has not explicitly defined interceptors configuration. See How do we configure an Interceptor to be used with every Action.

We can create our own named stacks and even declare a new default interceptor stack for a package

<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>

However, if you say that the interceptor above works fine I will encourage you to be cautious about a business logic that the login action will not be executed if it fails on the first execution. Instead of checking for the new session you should check for results of the authenticated user and save these results in the session that you could check in the interceptor. See this question for example.

The example of writing the interceptor that use the authenticated user information with the session you could find here.

Xl answered 5/6, 2013 at 12:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.