如何检查 Struts2 中名称空间内是否存在某个操作?

发布于 2024-09-12 14:32:43 字数 609 浏览 1 评论 0原文

我想在 Struts2 Web 应用程序中使用自己的 ActionMapper 实现某种 url“后备”。这意味着:

http://server/webapp/foo/bar/myaction

不存在,我想要ActionMapper 尝试加载例如

http://server/webapp/foo_fallback/bar/myaction

代替。

解析 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

http://server/webapp/foo/bar/myaction

does not exist, I want the ActionMapper to try to load e.g.

http://server/webapp/foo_fallback/bar/myaction

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无需解释 2024-09-19 14:32:43

您必须对每个配置的命名空间执行此操作:

<action name="*">
    <result type="redirectAction">your fallback action here</result>
</action>

如果不起作用,请将 struts.enable.SlashesInActionNames 设置为 false

You have to do this for every configured namespace:

<action name="*">
    <result type="redirectAction">your fallback action here</result>
</action>

If it doesn't work, set struts.enable.SlashesInActionNames to false.

没有心的人 2024-09-19 14:32:43

我解决了我的问题。这种情况发生在自定义 ActionMapper 中:

为了查明某个操作是否存在,我首先构造所需操作的类名(包括命名空间)的字符串。然后我调用

Class.forName("namespaceroot.foo.bar.myaction");

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

Class.forName("namespaceroot.foo.bar.myaction");

If the action does not exist, an ClassNotFoundException exception is thrown, which I can check for in a try { ... } catch { ... } block. Within the catch block I can change the namespace of the mapping to the fallback namespace. This has some implications to the ActionProxy: The namespace has to be changed there, too, in getMappingFromActionName. Otherwise the ActionProxy contains the namespace of the request, which is fine usually. The DefaultActionProxy does not have a setter for namespace, so I subclassed it and create it using a custom AxtionProxyFactory. 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文