views:

35

answers:

2

Hello,

I'd like to implement in a Struts2 web application some sort of url "fallback" using an own ActionMapper. This means:

when

http://server/webapp/foo/bar/myaction

does not exist, I want the ActionMapper to try to load e.g.

http://server/webapp/foo_fallback/bar/myaction

instead.

Parsing the URL and therefore finding the namespace is not a problem, but I don't know how to decide if the desired action is present in this namespace (which I have to modify if it is not).

Is there a possibility to check if an action exists within a namespace (/foo/bar in this case)? Or is there another mechanism to perform what I intend to do?

Thanks,

Gregor

A: 

You have to do this for every configured namespace:

<action name="*">
    <result type="redirectAction">your fallback action here</result>
</action>

If it doesn't work, set struts.enable.SlashesInActionNames to false.

Samuel_xL
If I understand you correctly this would perform a fallback action. What I need is a fallback /namespace/.However, I solved it using a custom ActionMapper and changing the namespace if needed in the getMapping() method. The ActionProxy has to get the new namespace as well, so I extended it by adding a setter for namespace. Hopefully this does not remind you of http://xkcd.com/763/ :)
gre
A: 

I solved my problem. This happens in the custom ActionMapper:

To find out if an action exists, I firstly construct a string of the class name (including the namespace) of the desired action. Then I call

Class.forName("namespaceroot.foo.bar.myaction");

If the action does not exist, an ClassNotFoundException exception is thrown, which I can check for in a try { ... } catch { ... } block. Within the catch block I can change the namespace of the mapping to the fallback namespace. This has some implications to the ActionProxy: The namespace has to be changed there, too, in getMappingFromActionName. Otherwise the ActionProxy contains the namespace of the request, which is fine usually. The DefaultActionProxy does not have a setter for namespace, so I subclassed it and create it using a custom AxtionProxyFactory. Phew.

This is not elegant imho, but as long as I don't come up with a better idea it'll stay that way. I'd love to hear a better solution!

gre