java struts2 在 struts.xml 中为所有动作类添加拦截器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16940190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:26:33  来源:igfitidea点击:

struts2 adding interceptors in struts.xml for all action class

javajakarta-eestruts2struts

提问by Java Beginner

I've used the Struts 2 framework and I have created a web application which has a Login Page. I have three different Actionclasses named Action1, Action2, Action3, and different views for JSP pages which are rendered by running some business logic in the Actionclasses.

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

Now, I want to check whether a user has logged in before the Actionclass carries out processing. So, I created an interceptor below that works fine.

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

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.xmlis instead of adding an interceptor for all the actions like the one below

我想要的是,struts.xml而不是为所有操作添加拦截器,如下所示

<interceptor-ref name="newStack"/>

My struts.xmlfile has

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

For every action I want to have some definition written in struts.xmlwhich runs the interceptor rather than manually adding

对于每个操作,我都希望编写一些定义struts.xml来运行拦截器而不是手动添加

<interceptor-ref name="newStack"/> 

回答by PSR

<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 testStackwill execute for every request

现在 testStack将为每个请求执行

回答by Roman C

Use

利用

<default-interceptor-ref name="newStack"/>

If you not putting interceptor-refmanually for each action you could use the default-interceptor-refto intercept all actions that has not explicitly defined interceptors configuration. See How do we configure an Interceptor to be used with every Action.

如果您没有interceptor-ref为每个操作手动放置,您可以使用default-interceptor-ref来拦截所有未明确定义拦截器配置的操作。请参阅我们如何配置与每个操作一起使用的拦截器

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>

我们可以创建自己的命名堆栈,甚至可以为包声明一个新的默认拦截器堆栈

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

编写使用经过身份验证的用户信息和会话的拦截器的示例,您可以在此处找到。