Search code examples
struts2strutsstruts-config

how to implement global redirect page in struts2


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.


Solution

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