Jetty URL重写/重定向条带查询字符串

时间:2014-03-27 13:49:25

标签: java jetty rewrite embedded-jetty

我目前正在与Jettys rewrite mechanisms斗争。基本上我想要URL     http://localhost:8080/membership/payment?id=5000取代http://localhost:8080/#/membership/payment?id=5000

我目前的做法如下:

RewriteHandler rewrite = new RewriteHandler();
rewrite.setRewriteRequestURI(true);
rewrite.setRewritePathInfo(true);
rewrite.setOriginalPathAttribute("requestedPath");

RewriteRegexRule replace = new RewriteRegexRule();
replace.setRegex("/membership/(.*)");
replace.setReplacement("/#/membership/$1");
rewrite.addRule(replace);

一般情况下这是有效的,唯一缺少的是要传递到新位置的查询字符串(?id = 5000)。

如何为新位置保留查询字符串?

1 个答案:

答案 0 :(得分:1)

好的,我找到了解决方法:

private static ContextHandlerCollection prepareContexts() {

    MovedContextHandler handler = new MovedContextHandler();
    handler.setContextPath("/membership");
    handler.setNewContextURL("/#/membership");
    handler.setPermanent(true);
    handler.setDiscardPathInfo(false);
    handler.setDiscardQuery(false);

    WebAppContext webAppContext = new WebAppContext();
    webAppContext.setServer(server);
    webAppContext.setContextPath("/");
    webAppContext.setWar("src/main/webapp");

    ContextHandlerCollection contexts = new ContextHandlerCollection();
    contexts.setHandlers(new Handler[] { handler, webAppContext });

    return contexts;
}

然后必须分配上下文:

server.setHandler(prepareContexts();