如何在 Struts 2 中将尾部斜杠 [/] 解析为 URL [英] How to resolve trailing slash [/] to the URL in Struts 2

查看:27
本文介绍了如何在 Struts 2 中将尾部斜杠 [/] 解析为 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

当我将所有内容部署到 Tomcat 并访问上面的 URL 时,我收到此错误:

没有为命名空间[/]和动作名称[home/]映射的动作";

显然 Tomcat 以某种方式在 URL 中添加了一个额外的 /,因此 Struts 认为操作名称是 home/ 而不是 home.

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

这是我的动作配置:

<结果名称=成功"类型=瓷砖"><param name="location">home.default</param><param name="menuItem">home</param></结果></动作>

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

<结果名称=成功"类型=瓷砖"><param name="location">home.default</param><param name="menuItem">home</param></结果></动作>

然后就可以了.

解决方案

Tomcat 正在添加尾部斜杠,因为您请求了一个目录.为了将目录与同名文件区分开来,Tomcat 添加了尾随斜杠.毫无疑问,包括 Jetty 在内的其他 Web 服务器也遵守此规则.否则他们将如何提供目录浏览和欢迎文件列表.另一方面,Struts 的默认动作映射器解析 URL 以从中确定动作映射.ActionMapper 接口,为您的操作创建映射.默认操作映射器使用最后一个斜杠将命名空间与操作名称分开.还有一些配置,您可以在操作名称中看到斜杠,如果配置设置允许,则在匹配操作名称后确定命名空间.如果将操作重命名为目录名称以外的名称,则可以轻松解决问题.请注意,convention plugin 在处理未知操作时会添加尾部斜杠.

网址末尾的斜杠是一种您可以按照规范化网址的样式.Struts 可以处理带有尾部斜杠和动作名称 "" 的映射.您还可以通过查看网址中的尾部斜杠 - 首选哪种样式来决定哪种样式更适合您.>

I have this project was working fine in Jetty, recently as requested, I'm testing it on Tomcat, but I found a problem.

We're using Struts 2, and the action mapping is defined with no extension, such as

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

when I get everything deployed into Tomcat and access the URL above and I got this error:

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

clearly somehow Tomcat added an extra / to the URL, so the Struts thinks the action name is home/ instead of home.

And if I changed the action config from home to home/, it works fine. But I don't want to change every action mapping with an extra /, there should be better solution.

Here is my configuration for the action:

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

it's configured under / package, when nothing changes, I got 404, and if I change above as:

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

then it works.

解决方案

The Tomcat is adding a trailing slash because you requested a directory. To distinguish a directory from the file with the same name the trailing slash is added by the Tomcat. Other web servers including Jetty also comply to this rule without doubt. Otherwise how would they serve directory browsing and welcome file list. On the other hand Struts' Default Action Mapper parses URL to determine Action Mapping from it. There are also different implementations of the 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 is adding a trailing slash when handling an unknown action.

Slashes at the end of the URL is a style that you can follow to 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.

这篇关于如何在 Struts 2 中将尾部斜杠 [/] 解析为 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆