Struts 2 - 使用任何 URL 的映射操作

2022-01-16 00:00:00 url java struts2 web.xml struts-action

我正在使用 Struts2 创建一个 Web 应用程序,但我在使用任何 url 的映射操作时遇到问题.

I am creating a web app with Struts2, and I am having an issue with the mapped actions working with any url.

在我的 struts.xml 文件中,我配置了一个带有/registration"命名空间的包,其中包含一些操作,主要的是注册".我的应用的上下文根是app/test".

In my struts.xml file, I have configured a package with a namespace of "/registration" with a few actions, with the main one being "register". The context root of my app is "app/test".

要访问注册表单,我可以转到localhost:8080/app/test/registration/register.action",它会加载我的表单并且效果很好.

To access the registration form, I can go to "localhost:8080/app/test/registration/register.action" and it loads up my form and works great.

但是,如果在命名空间之后的 URL 中添加任何内容,例如localhost:8080/app/test/registration/arbitrary/text/here/register.action",表单仍然会加载.

However, if anything is added to the URL after the namespace, such as "localhost:8080/app/test/registration/arbitrary/text/here/register.action", the form is still loaded up.

我想防止这种情况发生,以便您只能访问正确的 URL 表单.我在 struts.xml 和 web.xml 中尝试了许多不同的配置选项,但均无济于事,而且我无法在网上轻松找到有关此问题的知识.

I would like to prevent this from happening, so that you can only access the form the proper URL. I have tried many different configuration options in struts.xml and web.xml to no avail, and I cannot find knowledge on this issue easily on the web.

任何帮助将不胜感激,谢谢!

Any help will be appreciated, thanks!

struts.xml

<struts>
    <package name="myPackage" namespace="/registration" extends="struts-default">
        <result-types>
            <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult"/>
        </result-types>
        <action name="register" class="edu.uconn.test.action.RegistrationAction" method="input">
            <result name="input" type="tiles">/register.tiles</result>
        </action>
    </package>
</struts>

推荐答案

struts.mapper.alwaysSelectFullNamespace 常量设置为 true:

<constant name="struts.mapper.alwaysSelectFullNamespace" value="true" />

当利用 S2 对 URL 中任意参数的支持(例如,通配符、正则表达式模式匹配)时,这可能会产生意想不到的后果.

This may have unintended consequences when leveraging S2's support for arbitrary parameters in URLs (e.g., wildcarding, regex pattern matching).

相关文章