在struts.xml中为所有Action类添加拦截器 [英] Adding interceptors in struts.xml for all Action classes

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

问题描述

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

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,

我在下面创建了一个工作正常的拦截器:

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 文件具有:

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>

对于每个动作,我想在 struts.xml 中编写一些定义,该定义运行拦截器,而不是手动添加

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 将针对每个请求执行

Now testStack will execute for every request

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

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