我在将遗留代码移动到新实现的过程中。传统代码由服务器托管,该服务器具有“L”前缀为“遗留”。随着每个页面移动到新服务器,我希望URLREWRITEFILTER更改URL。所以我需要做这样的事情:

from: http://lapp.company.com/page1.aspx to: http://app.company.com/pageone

情况进一步复杂,因为这需要在每个案例中稍微发生多种不同的环境(开发,阶段,生产)。例如:

本地: http://applocal.companydev.com:8080 dev: http://app.companydev.com 阶段: http://app.com panystage.com prod: http://app.company.com

这是一个示例规则:

  <rule>
    <from>^/offers/buyKit.aspx$</from>
    <to type="redirect">%{context-path}/offers/buyKit</to>
  </rule>
.

http://lapp.company.com/offers/buykit.aspx 进来,我希望它更改为 http://app.company.com/offers/buykit

urlrewritefilter支持这种事情,或者我逃脱了吗?

有帮助吗?

解决方案

这里是我想出的解决方案。 重写过滤器允许您指定在规则匹配时将调用的方法。我使用此回调方法根据当前环境查找服务器上下文。然后将此环境确定为请求的属性,该属性可以由规则的“to”元素使用。

以下是XML文件的更新规则:

<rule>
    <from>^/offers/buyKit.aspx$</from>
    <run class="com.company.app.UrlHelper" method="setMyContext"/>
    <to type="redirect" last="true">%{attribute:myContext}/offers/buyKit</to>
</rule>
.

以及以下是上下文设置方法:

public class UrlHelper {

    private static final String APP_BASE_URL = "app.company.base.url";
    private Properties appConfig;

    public void setMyContext(HttpServletRequest request, HttpServletResponse response) throws IOException {
        if (appConfig == null) {
            appConfig = (Properties) StaticSpringApplicationContext.getBean("appConfig");
            Assert.notNull(appConfig, "Unable to get appConfig.");
        }
        String appBase = appConfig.getProperty(APP_BASE_URL);
        Assert.hasText(appBase, "Could not find property: " + APP_BASE_URL);
        request.setAttribute("myContext", appBase);
    }
}
.

它很好。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top