tags:

views:

93

answers:

1

I have a problem finding the right wildcard pattern to extract parts of my URL into action parameters in Struts.

This is how I set up the action. The intent of the pattern is to capture the last two path elements and then everything that might precede them.

<action name="**/*/*" class="com.example.ObjectAction">
  <param name="filter">{1}</param>
  <param name="type">{2}</param>
  <param name="id">{3}</param>
</action>

Calling it with the URL channels/123/transmissions/456 I get the following result (the action just sets the input parameters on a POJO and returns that as XML):

<result>
    <filter>channels/123/transmissions</filter>
    <id/>
    <type>456</type>
</result>

It should be:

<result>
    <filter>channels/123</filter>
    <id>456</id>
    <type>transmissions</type>
</result>

Now, because ** matches all characters including the slash, I guess my pattern allows more than one way to match the URL, and Struts happens to pick one that leaves the id empty. Is the behaviour for multiple possible matches defined somewhere? Can I make the pattern less ambigous? Are there alternative ways of doing this?

I'm running Struts 2.0.8. Upgrading to 2.1.9 would give me regex matching, but I got into trouble with Struts' dependencies and my OSGi environment when I went past 2.0.8, so I'd like to stick to that version for now.

A: 

This is more general advice than specific to structs, but here goes.

If you append an additional / to the URL (Which could be done automatically at some point in your testing structure), then the greedy result would be what you are seeing.

I would double check to ensure in some way that you are requesting the URL you think you are, or see if there is a way to specify + (aka a non-empty result)

EDIT: To answer the overall question, it is hard to tell whether a match is greedy or not, since often times parsers will return the first "valid" result, which may be equivalent to the greedy one depending on where you have it.

Guvante
Yes, I checked that I'm calling the URL I think I am. Appending "/" leads to the whole path ending up in `filter`. I think my problem depends a lot on some details in how Struts handles this.
Hanno Fietz
I rephrased my question almost completely after I got a clearer picture of what my problem is. Sorry if that made your answer look weird.
Hanno Fietz
If 2.1.9 is giving a better error message, then try getting it to work in that version then backport, if it is using the same logic with some additional checks then this trick should work. If it is a different method, then obviously it won't
Guvante
Guvante, there is no error message. The URL is parsed, and, from a technical point of view, correctly. It just isn't working as I intend it to.
Hanno Fietz