Interceptor not getting called in Struts

假装没事ソ 提交于 2019-12-12 22:11:45

问题


My interceptor (validation) is not getting called before or after the action. Any ideas how to get it work ?

Note : Everytime the default interceptor is being called.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
  <package name="default" namespace="/" extends="struts-default,json-default">
    <result-types>
      <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" />
      <result-type name="json" class="org.apache.struts2.json.JSONResult" />
    </result-types>

    <interceptors>
       <interceptor name="validation" class="ValidatorBaseAction"/>
      <interceptor-stack name="default">
        <interceptor-ref name="logger"/>
      </interceptor-stack>
      <interceptor-stack name="validationStack">
         <interceptor-ref name="validation"/>
         <interceptor-ref name="default"/>
      </interceptor-stack>
    </interceptors>

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

    <action
        name="viewRequest"
        class="ViewAction"
        method="execute">
      <interceptor-ref name="validationStack" /> 
      <result name="input" type="redirectAction">explore</result>
      <result name="success" type="redirect">/showRequest.do?${explorerParameters}</result>
    </action>
  </package>
</struts>

回答1:


Main problem:

  • class both for Actions and Interceptors must specify the FQCN, not only the name. Then change it to something like:

    <interceptor name="validation" class="com.foo.bar.ValidatorBaseAction"/>
    

    and also change your action to

    <action name="viewRequest" class="com.foo.bar.ViewAction" method="execute">
    

Side problems:

  • Don't call it ValidatorBaseAction if it is an Interceptor, call it ValidatorBaseInterceptor. And ensure there is nothing of an Action inside it;
  • Don't use an Interceptor Stack with only one Interceptor, I'm pretty sure it would be useless in 99% of the cases. If you are not sure, just use the defaultStack, adding your interceptor to it.

Polishing:

  • json-default already extends struts-default, so this

    <package ... extends="struts-default,json-default"
    

    is equivalent to this

    <package ... extends="json-default"
    

    that is cleaner;

  • Since you extends json-default, you don't need to redefine the JSON result, then remove

    <result-type name="json" class="org.apache.struts2.json.JSONResult" />
    

    that is useless.

  • Try always to prefer redirectAction result when redirecting to an Action, and use redirect result only when redirecting to other resources or external URLs



来源:https://stackoverflow.com/questions/30043007/interceptor-not-getting-called-in-struts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!