struts2wildcard-mapping

Underscores and wildcard action mapping in Struts 2


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>

Solution

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