Java中如何访问struts拦截器参数?
我在 struts.xml 中有以下代码:
<interceptor-ref name="checkTabsStack">
<param name="tabName">availability</param>
</interceptor-ref>
并且我想访问拦截器例程中的参数 tabName,我该怎么做?我尝试过
Map params = ActionContext.getContext().getParameters();
,但参数为空...
谢谢!
I have the following code in struts.xml:
<interceptor-ref name="checkTabsStack">
<param name="tabName">availability</param>
</interceptor-ref>
and I want to access the parameter tabName in the interceptor routine, how do i do that? i tried
Map params = ActionContext.getContext().getParameters();
but params comes empty...
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Interceptor 对象不应该知道用于创建/配置它的 xml 片段,这是 Struts2 内部的东西。想一想:一个拦截器类(eg) 没有某些“params”属性,它甚至可能(理论上)通过某种与您发布的 struts.xml 无关的机制实例化。这是解耦,Struts2 非常重视这一点。
确实,有些拦截器有一些可配置的参数;但它们通常是特定类的属性。在您的示例中,您的特定拦截器类(您应该知道)可能具有方法
setTabName()
getTabName()
。 setter将被struts2在读取struts.xml文件并实例化拦截器时调用。 getter 是你应该关注的。查看您的拦截器类文档。An Interceptor object should know nothing about the piece of xml that was used to create/configurate it, that is something internal to Struts2. Think of it: a interceptor class (eg) does not have some "params" attribute, and it might even be (in theory) instanced by some mechanism that has nothing to do with the struts.xml you post. This is decoupling, and Struts2 takes that seriously.
True, some interceptors have some configurable parameters; but they will normally be properties of the particular class. In your example, your particular interceptor class (which you should know) might have probably the methods
setTabName()
getTabName()
. The setter will be called by struts2 when reading the struts.xml file and instantiating the interceptor. The getter is what you should be looking after. Look into your interceptor class docs.在您的拦截器类中:
当拦截器初始化时,Struts 将调用此 setter。
In your interceptor class:
Struts will call this setter when the interceptor is initialized.