简体   繁体   English

如何在 Wicket 1.5 中模仿 HybridUrlCodingStrategy?

[英]How do I mimic HybridUrlCodingStrategy in Wicket 1.5?

We have an existing Java Wicket 1.4 application which uses the HybridUrlCodingStrategy extensively:我们有一个现有的 Java Wicket 1.4 应用程序,它广泛使用 HybridUrlCodingStrategy:

mount(new HybridUrlCodingStrategy("/myurl", MyPage.class));

This results in our URL's looking like:这导致我们的 URL 看起来像:

http://host/myurl/paramName1/paramValue1/paramName2/paramValue2

I would like to maintain this URL format in Wicket 1.5, however the HybridUrlCodingStrategy has been removed.我想在 Wicket 1.5 中维护这个 URL 格式,但是 HybridUrlCodingStrategy 已被删除。 In wicket 1.5, pages are mounted as:在 wicket 1.5 中,页面安装为:

mountPage("/myurl", MyPage.class);

Which results in traditional URLs like:这导致了传统的 URL,例如:

http://host/myurl?paramName1=paramValue2&paramName2=paramValue2

I have read that we should be using the MountedMapper class, but looking at the Wicket 1.5 examples, API docs, and source code, it is still not clear to me how to get the same behavior with MountedMapper as we are getting with the HybridUrlCodingStrategy.我已经读到我们应该使用 MountedMapper class,但是查看 Wicket 1.5 示例、API 文档和源代码,我仍然不清楚如何使用 MountedMapper 获得与使用 HybridUrlCodingStrategy 相同的行为。

Does anyone know how to do this?有谁知道如何做到这一点?

Maybe something like this:也许是这样的:

mountPage("/myurl/paramName1/${paramValue1}/paramName2/${paramValue2}", MyPage.class)

would work?会工作? Granted, you'd have to manually specify your parameters, which could be a lot more work.当然,您必须手动指定参数,这可能需要更多工作。 The MountedMapper class javadoc explains how to use parameters. MountedMapper class javadoc 解释了如何使用参数。

The other option I can think of would be ( Note : this is untested):我能想到的另一个选择是(注意:这是未经测试的):

class MyPageParametersEncoder implements IPageParametersEncoder() {
    public PageParameters decodePageParameters(Request request)
    {
        PageParameters parameters = new PageParameters();

        int i = 0;
        for (Iterator<String> segment = request.getUrl().getSegements().iterator(); segment.hasNext()) {
            String key = segment.next();
            String value = segment.next();

            parameters.add(key, value);
        }

        return parameters.isEmpty() ? null : parameters;
    }

    public Url encodePageParameters(PageParameters pageParameters)
    {
        Url url = new Url();

        for (PageParemeters.NamedPair pair : pageParameters.getAllNamed() {
            url.getSegments().add(pair.getKey());
            url.getSegments().add(pair.getValue());
        }

        return url;
    }
}

mount(new MountedMapper("/myurl/", MyPage.class, new MyPageParametersEncoder());

No need of custom IPageParametersEncoder.无需自定义 IPageParametersEncoder。

With mountPage("/myurl/paramName1/${paramValue1}/paramName2/${paramValue2}", MyPage.class) the URL will look like in 1.4 but the values will be reachable as StringValue value1 = parameters.get("paramValue1").使用 mountPage("/myurl/paramName1/${paramValue1}/paramName2/${paramValue2}", MyPage.class) URL 看起来像在 1.4 中,但值可以通过 StringValue value1 = parameters.get("paramValue1" )。 Similar for value2. value2 类似。

With mountPage("/myurl/${paramValue1}/${paramValue2}", MyPage.class) is the same according to extracting the values, just shorter URL will be used.与 mountPage("/myurl/${paramValue1}/${paramValue2}", MyPage.class) 根据提取值相同,只是使用较短的 URL 。

It also supports optional paramaters - #{optionalValue3}.它还支持可选参数 - #{optionalValue3}。

NOTE: A new class has been added to Wicket 1.5.2 for backwards compatibility with 1.4 style URL encoding.注意:Wicket 1.5.2 中添加了新的 class 以向后兼容 1.4 样式 URL 编码。 It's called UrlPathPageParametersEncoder - use that if you're migrating a wicket 1.4 app to 1.5 and you have bookmarkable page links of the style:它被称为 UrlPathPageParametersEncoder - 如果您将 wicket 1.4 应用程序迁移到 1.5 并且您有以下样式的可收藏页面链接,请使用它:

www.mysite.com/name1/value1/name2/value2 www.mysite.com/name1/value1/name2/value2

We had the exact same issue when migrating from 1.4 to 1.5.从 1.4 迁移到 1.5 时,我们遇到了完全相同的问题。 Any 1.4 app that has been live for a while would likely have a collection of links pointing to it from external sites on the web.任何已经存在一段时间的 1.4 应用程序都可能有一组从 web 上的外部站点指向它的链接。 You really want the Wicket 1.5 version of your app to be able to handle these existing hybrid links without generating an error.确实希望您的应用程序的 Wicket 1.5 版本能够处理这些现有的混合链接而不会产生错误。

When migrating to 1.5, without a 1.4 compatible IPageParametersEncoder implementation, you need to include the full parameter specification in every mount if you want to avoid making changes to each individual Page class that reads parameters.迁移到 1.5 时,如果没有与 1.4 兼容的 IPageParametersEncoder 实现,如果您想避免对读取参数的每个单独页面 class 进行更改,则需要在每次挂载中包含完整的参数规范。 The implementation below means that is no longer necessary.下面的实现意味着不再需要。 Just mount the page as livid suggests above.只需按照上面的 livid 建议安装页面即可。

I'm submitting this.java file as a patch to the Wicket devs - they may include it in Wicket in the future to make it easy to implement backwards compatible URL parameter encoding for other 1.4 migrators.我将 this.java 文件作为补丁提交给 Wicket 开发人员 - 他们将来可能会将其包含在 Wicket 中,以便为其他 1.4 迁移器实现向后兼容的 URL 参数编码。

I took luniv's sample code above and made a few small changes to get it compiling/working.我采用了上面 luniv 的示例代码并进行了一些小改动以使其编译/工作。 The following should work as a parameter encoder to provide 1.4.x style parameter encoding in 1.5.以下应作为参数编码器在 1.5 中提供 1.4.x 样式参数编码。

    package org.apache.wicket.request.mapper.parameter;

    import java.lang.*;

    import org.apache.wicket.request.mapper.parameter.IPageParametersEncoder;

    import java.util.Iterator;

    import org.apache.wicket.request.Request;

    import org.apache.wicket.request.Url;

    import org.apache.wicket.request.mapper.parameter.PageParameters;

    public 
    class HybridPageParametersEncoder implements IPageParametersEncoder
    {
    /**
     * Encodes a URL in the form:
     * 
     * /mountpoint/paramName1/paramValue1/paramName2/paramValue2 
     * 
     * (i.e. a URL using the pre wicket 1.5 Hybrid URL strategy)
     */
    public Url encodePageParameters(PageParameters pageParameters)
    {
        Url url = new Url();

        for (PageParameters.NamedPair pair : pageParameters.getAllNamed())
        {
            url.getSegments().add(pair.getKey());
            url.getSegments().add(pair.getValue());
        }

        return url;
    }

    /**
     * Decodes a URL in the form:
     * 
     * /mountpoint/paramName1/paramValue1/paramName2/paramValue2 
     * 
     * (i.e. a URL using the pre wicket 1.5 Hybrid URL strategy)
     */
    public PageParameters decodePageParameters(Request request)
    {
        PageParameters parameters = new PageParameters();

        int i = 0;
        for (Iterator<String> segment = request.getUrl().getSegments().iterator(); segment.hasNext(); )
        {
            String key = segment.next();
            String value = segment.next();

            parameters.add(key, value);
        }

        return parameters.isEmpty() ? null : parameters;
    }
}

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

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