Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
341 views
in Technique[技术] by (71.8m points)

java - 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"/> 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
<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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...