如何将尾部斜杠 [/] 解析为 Struts 中的 URL 2

How to resolve trailing slash [/] to the URL in Struts 2

我有这个项目在 Jetty 中运行良好,最近根据要求,我正在 Tomcat 上测试它,但我发现了一个问题。

我们使用的是Struts 2,并且定义的动作映射没有扩展名,例如

http://www.somehost.com/projectname/home

当我将所有内容部署到 Tomcat 并访问上面的 URL 时,我得到了这个错误:

"There is no Action mapped for namespace [/] and action name [home/]"

显然 Tomcat 向 URL 添加了一个额外的 /,所以 Struts 认为动作名称是 home/ 而不是 [=18] =].

如果我将操作配置从 home 更改为 home/,它会正常工作。但是我不想用额外的/来改变每个动作映射,应该有更好的解决方案。

这是我的操作配置:

<action name="home" class="com.hp.bpm.portal.action.EmptyAction">
    <result name="success" type="tiles">
      <param name="location">home.default</param>
      <param name="menuItem">home</param>
    </result>
</action>

它是在/包下配置的,当没有任何变化时,我得到404,如果我在上面更改为:

<action name="home/" class="com.hp.bpm.portal.action.EmptyAction">
    <result name="success" type="tiles">
      <param name="location">home.default</param>
      <param name="menuItem">home</param>
    </result>
</action>

然后就可以了。

Tomcat 添加了尾部斜杠,因为您请求了一个目录。为了将目录与同名文件区分开来,Tomcat 添加了尾部斜线。毫无疑问,包括 Jetty 在内的其他 Web 服务器也遵守此规则。否则他们将如何提供目录浏览和欢迎文件列表。另一方面,Struts' Default Action Mapper 解析 URL 以从中确定 Action Mapping。 ActionMapper interface that create mapping for your action. The default action mapper uses the last slash to separate the namespace from the action name. There are also configurations where you can see slashes in the action name, if the configuration setting allows this then the namespace is determined after matching the action name. You can easy solve your problem if you rename your action to something other than directory name. Note that convention plugin 也有不同的实现方式是在处理未知操作时添加尾部斜线。

URL 末尾的斜杠是一种您可以遵循 normalize URL. Struts can handle mapping with the trailing slash and action name "". You can also decide which style is better suited to you by looking at Trailing slash in URLs - which style is preferred 的样式。