问题
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 anInterceptor
, call itValidatorBaseInterceptor
. 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 useredirect
result only when redirecting to other resources or external URLs
来源:https://stackoverflow.com/questions/30043007/interceptor-not-getting-called-in-struts