struts2在struts.xml中为所有动作类添加拦截器 [英] struts2 adding interceptors in struts.xml for all action class

查看:112
本文介绍了struts2在struts.xml中为所有动作类添加拦截器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用过Struts 2框架,并创建了一个具有登录页面的Web应用程序。我有三个不同的 Action 类名为 Action1 Action2 Action3 ,以及通过在 Action 类中运行某些业务逻辑而呈现的JSP页面的不同视图。

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.

现在,我想在 Action 类执行处理之前检查用户是否已登录。所以,我在下面创建了一个可以正常工作的拦截器。

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

我想要的是什么 struts.xml 而不是为下面的所有操作添加拦截器

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

我的 struts.xml 文件有

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

对于每个动作,我想要用 struts.xml <写一些定义/ code>运行拦截器而不是手动添加

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


推荐答案

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

现在 testStack 将为每个请求执行

这篇关于struts2在struts.xml中为所有动作类添加拦截器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆