如何检查 Struts2 中名称空间内是否存在某个操作?
我想在 Struts2 Web 应用程序中使用自己的 ActionMapper 实现某种 url“后备”。这意味着:
当
不存在,我想要ActionMapper 尝试加载例如
代替。
解析 URL 并因此找到名称空间不是问题,但我不知道如何确定此名称空间中是否存在所需的操作(如果不存在,我必须对其进行修改)。
是否可以检查命名空间中是否存在某个操作(在本例中为 /foo/bar)?或者是否有另一种机制来执行我打算做的事情?
谢谢,
格雷戈尔
I'd like to implement in a Struts2 web application some sort of url "fallback" using an own ActionMapper. This means:
when
does not exist, I want the ActionMapper to try to load e.g.
instead.
Parsing the URL and therefore finding the namespace is not a problem, but I don't know how to decide if the desired action is present in this namespace (which I have to modify if it is not).
Is there a possibility to check if an action exists within a namespace (/foo/bar in this case)? Or is there another mechanism to perform what I intend to do?
Thanks,
Gregor
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须对每个配置的命名空间执行此操作:
如果不起作用,请将
struts.enable.SlashesInActionNames
设置为false
。You have to do this for every configured namespace:
If it doesn't work, set
struts.enable.SlashesInActionNames
tofalse
.我解决了我的问题。这种情况发生在自定义 ActionMapper 中:
为了查明某个操作是否存在,我首先构造所需操作的类名(包括命名空间)的字符串。然后我调用
If the action does notise, an
ClassNotFoundException
异常被抛出,我可以在try { ... } catch { ... }
块中检查该异常。在catch
块中,我可以将映射的命名空间更改为后备命名空间。这对ActionProxy
有一些影响:在getMappingFromActionName
中,命名空间也必须更改。否则,ActionProxy
包含请求的名称空间,这通常没问题。DefaultActionProxy
没有命名空间的 setter,因此我将其子类化并使用自定义AxtionProxyFactory
创建它。唷。恕我直言,这并不优雅,但只要我没有想出更好的主意,它就会保持这种状态。我很想听到更好的解决方案!
I solved my problem. This happens in the custom ActionMapper:
To find out if an action exists, I firstly construct a string of the class name (including the namespace) of the desired action. Then I call
If the action does not exist, an
ClassNotFoundException
exception is thrown, which I can check for in atry { ... } catch { ... }
block. Within thecatch
block I can change the namespace of the mapping to the fallback namespace. This has some implications to theActionProxy
: The namespace has to be changed there, too, ingetMappingFromActionName
. Otherwise theActionProxy
contains the namespace of the request, which is fine usually. TheDefaultActionProxy
does not have a setter for namespace, so I subclassed it and create it using a customAxtionProxyFactory
. Phew.This is not elegant imho, but as long as I don't come up with a better idea it'll stay that way. I'd love to hear a better solution!