在 struts.xml 中为所有 Action 类添加拦截器

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

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

问题描述

我使用了 Struts 2 框架并创建了一个具有登录页面的 Web 应用程序.我有三个不同的 Action 类,分别命名为 Action1Action2Action3,以及用于呈现的 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 类添加拦截器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 struts.xml 中为所有 Action 类添加拦截器

基础教程推荐