Struts2拦截器参数及其生命周期
我们在代码中使用 Struts2,我有一个场景需要向拦截器添加自定义参数。 我在拦截器中定义了 setter 和 getter
public Collection<Class<?>> getGroups();
public void setGroups(String groupsString);
我在堆栈中定义了拦截器:
<interceptor-ref name="beanValidatorInterceptor">
<param name="excludeMethods">*</param>
</interceptor-ref>
并且我在操作级别设置附加参数
<action name="...">
<interceptor-ref name="loginRequired">
<param name="beanValidatorInterceptor.includeMethods">execute</param>
<param name="beanValidatorInterceptor.groups">com.company.MyGroup</param>
</interceptor-ref>
<result>some.jsp</result>
</action>
我有一个问题和一个问题
- 问题:未设置组参数(未调用 setGroups() )。调用拦截器并设置 includeMethods。
- 问题:你知道什么是拦截器生命周期吗?在操作级别设置 includeMethods 和组是否安全。换句话说,动作共享相同的堆栈,但需要传递不同的参数
we are using Struts2 in our code and I have scenario where I need to add custom parameter to the interceptor.
I have defined setter and getter in the interceptor
public Collection<Class<?>> getGroups();
public void setGroups(String groupsString);
I have interceptor defined in the stack:
<interceptor-ref name="beanValidatorInterceptor">
<param name="excludeMethods">*</param>
</interceptor-ref>
And I am setting additional parametres on action level
<action name="...">
<interceptor-ref name="loginRequired">
<param name="beanValidatorInterceptor.includeMethods">execute</param>
<param name="beanValidatorInterceptor.groups">com.company.MyGroup</param>
</interceptor-ref>
<result>some.jsp</result>
</action>
I have one issue and one question
- Issue: groups parameters is not set (setGroups() is not called). Interceptor is called and the includeMethods are set.
- Question: do you happen to know what is interceptor lifecycle. Is it safe to set includeMethods and groups on the action level. In other words actions are sharing same stack but need to pass different parameters
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,也不是。拦截器就像 servlet — 拦截器只有一个实例。*
但请注意,根据您当前的定义,仅该操作上定义了“loginRequired”拦截器 —无 其他框架拦截器将运行。如果您在操作上声明
,则必须定义所有拦截器。从配置来看,您似乎正在尝试定义哪些组可以访问某个操作:该信息应该存储在操作中,而不是拦截器中,因为它是正在检测的操作。
您有多种选择。您可以使用“staticParams”拦截器来设置操作的信息,您可以定义和实现操作的接口并包含检索该操作的权限的方法,或者在操作上使用注释并询问操作和/或拦截器内的操作方法。
IMO 拦截器应该是通用的,并且不接收这些类型的参数:您正在向拦截器添加操作元数据。相反,将操作元数据添加到操作中,并使用拦截器对该元数据进行操作。
*实际上,根据拦截器引用;拦截器可以有多个实例,但仅当在堆栈声明中显式引用时才有效,即使如此,流经该堆栈的请求之间也只会共享一个拦截器实例。
Yes and no. Interceptors are like servlets--there is only one instance of an interceptor.*
Note, however, that with your current definition, only the "loginRequired" interceptor is defined on that action--none of the other framework interceptors will be run. If you declare an
<interceptor-ref>
on an action, you must define all the interceptors.From the config it looks as though you're trying to define what group may access an action: that information should be stored in the action, not the interceptor, since it is the action that's being instrumented.
You have several options. You may use the "staticParams" interceptor to set the information on the action, you may define and implement an interface on the action and include a method that retrieves the permissions for that action, or use annotations on the action and interrogate the action and/or action method within the interceptor.
IMO the interceptor should be generic and not receive these types of parameters: you are adding action metadata to an interceptor. Instead, add the action metadata to the action, and use the interceptor to act on that metadata.
*Per interceptor-ref, actually; there can be multiple instances of an interceptor, but only when explicitly referenced in a stack declaration, and even then, there will be only one instance of the interceptor shared among requests that flow through that stack.