简体   繁体   中英

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 eg /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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM