3

I'm working with a Struts 2 webapp with the following action mapping:

<action name="something_*" class="foo.whatever.MyAction" method="{1}">
  <result>blah/myJsp.jsp</result>
  ...
</action>

So if I load the URL /something_load.action, it calls to MyAction.load(), and so on. Piece of cake. What puzzles me is that loading /something.action does work too (I guess it's invoking the execute() method). How is it possible? My action mapping is supposed to match "something_", but there is no underescore in my URL. It should give me an error! Shouldn't it?

I've double checked that there isn't another mapping for "something.action" in the struts config files. I also checked the web.xml file, just in case...

The only explanation that I can imagine is that the underscore is ignored in Struts if I use wildcard mappings. But then it would make no difference to load /something_load.action, /some_thing_lo_ad.action... and that's not true.

I'm aware that this must be a very noobish question, but I've been unable to solve the mistery, neither looking through Stackoverflow questions nor the Struts documentation.

This is the main struts.xml file:

<struts>
  <constant name="struts.enable.DynamicMethodInvocation" value="false" />
  <constant name="struts.devMode" value="false" />
  <constant name="struts.freemarker.templatesCache" value="true" />

  <package name="default" extends="struts-default">
    <!-- interceptors ... -->
    <!-- global results for error pages -->
  </package>

  <!-- lots of includes -->
</struts>
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
AJPerez
  • 3,435
  • 10
  • 61
  • 91
  • It does seem the last underscore is getting "trimmed" (while, obviously, those in the middle of the action names are not). Odd stuff, +1 – Andrea Ligios Mar 27 '14 at 10:17
  • @AndreaLigios thanks for the edit, I was wondering why my XML was showing without syntax highlighting :) – AJPerez Mar 27 '14 at 10:34
  • Yes, if the lang can't be inferred exactly by the tags, you need to add that syntax highlighter code (it wouldn't be right to tag the question as "xml" just to have it formatted :) – Andrea Ligios Mar 27 '14 at 10:37
  • Maybe it has something to do with `default-action-ref` or index.action? `ActionSupport` provides `execute()` method with default `SUCCESS` result and it will be used as a default action. Have you tried prepare a small demo app to reproduce the problem? – Lukasz Lenart Mar 27 '14 at 11:26
  • It appears that wildcards are matched loosely. `Patterns can optionally be matched "loosely". When the end of the pattern matches \*[^*]\*$ (wildcard, no wildcard, wildcard), if the pattern fails, it is also matched as if the last two characters didn't exist. The goal is to support the legacy "*!*" syntax, where the "!*" is optional.` – Aleksandr M Mar 27 '14 at 11:55
  • 1
    This answer should help you http://stackoverflow.com/a/17673308/573032 – Roman C Mar 27 '14 at 20:36

1 Answers1

1

It appears that wildcards are matched loosely in order to support some legacy syntax. So the issue isn't with underscore but with loose matching pattern.

From the javadocs:

Patterns can optionally be matched "loosely". When the end of the pattern matches \*[^*]\*$ (wildcard, no wildcard, wildcard), if the pattern fails, it is also matched as if the last two characters didn't exist. The goal is to support the legacy "*!*" syntax, where the "!*" is optional.

AJPerez
  • 3,435
  • 10
  • 61
  • 91
Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
  • I saw that paragraph on the Struts 2.3 documentation. It's not present on the equivalent page for Struts 2.0.x, which is the version we are using. I guess it behaves exactly the same, despite not being mentioned on the docs... I wanted to run a little test to be completely sure, but I haven't had time to do it (That's why it took me so long to accept the answer, sorry about that :) – AJPerez Apr 23 '14 at 18:32