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

1

1

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.

Platinumblond answered 3/1, 2015 at 10:0 Comment(5)
Please show your configuration; AFAIK Tomcat won't add anything to URL you didn't ask it to.Wildee
i've added my configuration, please take a look again, thanksPlatinumblond
Is it a portlet? What's the package configuration look like? What specifically is the action extension configuration? I mean, this works fine under Tomcat, it's how almost every S2 webapp ever is configured. There's missing information. Also, which version of S2, and Tomcat?Wildee
i tried Tomcat 7&8, and tried the action extension configuration as : <constant name="struts.action.extension" value="" /> <constant name="struts.action.extension" value="," /> struts version is 2.3.15.3Platinumblond
if i configured the action with extension, such as .do, .action, no problem at all, but for some reasons, our project requires the action url without any extension. The same codes and configurations were tested under Jetty and it works fine, when i deployed the same codes to Tomcat, i got this problem.Platinumblond
L
1

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.

Lorrinelorry answered 3/1, 2015 at 20:30 Comment(1)
thanks a lot, that resolved my problem, turns out we have this folder with same name as 'home' under the root directory, once i changed it as different to action name, problem was gone, thanks again.Platinumblond

© 2022 - 2024 — McMap. All rights reserved.