简体   繁体   English

如何在struts2中实现全局重定向页面

[英]how to implement global redirect page in struts2

We're using Struts 2 in our web application. 我们在Web应用程序中使用Struts 2。 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. 已经定义了许多动作映射,我想实现一个功能,其中任何以/ buy开头且未映射到任何现有动作映射(例如/ buy / newbuy / old)的Urls都应重定向到Buy / index动作。 For example if someone is trying to go to /buy/bla1 or /buy/bla2 or buy/bla1/bla2/bla3 should go to buy/index. 例如,如果有人尝试去/ buy / bla1/ buy / bla2buy / bla1 / bla2 / bla3应该去购买/索引。

Define the following action in your /buy package: /buy包中定义以下操作:

<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.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. 空字符串包含/ buy / bla之类的目录,但/buy/bla.x不会被涵盖,并且会产生404错误,除非您将x添加到扩展列表中。

If this is not good enough you might be better off solving this by using redirect rules in the webserver. 如果这还不够好,您最好通过使用Web服务器中的重定向规则来解决此问题。

You can use default action reference and provide a JSP success path to it. 您可以使用默认操作引用,并为其提供JSP成功路径。

If any unmatched action is received then it will call the given action and forward it to the result JSP page. 如果收到任何不匹配的动作,它将调用给定的动作并将其转发到结果JSP页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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