Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding interceptors in struts.xml for all Action classes

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"/> 
like image 486
Java Beginner Avatar asked Jun 05 '13 12:06

Java Beginner


2 Answers

<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

like image 97
PSR Avatar answered Sep 19 '22 01:09

PSR


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.

like image 38
Roman C Avatar answered Sep 19 '22 01:09

Roman C