We're using Struts 2 in our web application. There are bunch of action mappings defined already, I want to implement a feature where any Urls starting with /buy and not mapped to any of the existing action mappings e.g. /buy/new or buy/old should be redirected to buy/index action. For example if someone is trying to go to /buy/bla1 or /buy/bla2 or buy/bla1/bla2/bla3 should go to buy/index.
2 Answers
Define the following action in your /buy
package:
<action name="*">
<result type="redirectAction">index</result>
</action>
Alternatively you can just use:
<default-action-ref name="index" />
But then you will get no redirect. Instead the index page will show under the nonexisting address.
You also need to set the following parameter in your struts.properties:
struts.action.extension = action,,
You can add other extensions to the parameter, but as far as i know there is no wildcard for all extensions. The blank string covers directories like /buy/bla but /buy/bla.x won't be covered and will produce a 404 error unless you add x to the list of extensions.
If this is not good enough you might be better off solving this by using redirect rules in the webserver.

- 190
- 7
-
Struts2 already has a default action, and why to configure this weird extension which stops struts from loading static pages. – Roman C May 22 '15 at 18:44
-
I completely forgot about the struts default action as i never used it. I removed the unnecessary step from the answer. I also added the possible usage of `default-action-ref`. – Stefan Bartel May 22 '15 at 19:38
-
1The weird extension originates from work. Our client doesn't want to telegraph which framework is used and so we had to use **html** as action extension. I changed it to **action** to lower the confusion. – Stefan Bartel May 22 '15 at 19:41
-
If you or your client X *doesn't want to telegraph which framework is used* better to not use a framework at all, and extension used doesn't point directly on the framework usage, rather gives an extension to the action mapping. OP doesn't use any extension at all, and you should follow him at least. – Roman C May 22 '15 at 19:54
You can use default action reference and provide a JSP success path to it.
If any unmatched action is received then it will call the given action and forward it to the result JSP page.

- 158,873
- 26
- 254
- 302

- 104
- 1
- 9