如何在 Wicket 1.5 中模仿 HybridUrlCodingStrategy?

发布于 2024-11-10 17:01:43 字数 678 浏览 0 评论 0原文

我们有一个现有的 Java Wicket 1.4 应用程序,它广泛使用 HybridUrlCodingStrategy:

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

这导致我们的 URL 看起来像这样:

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

我想在 Wicket 1.5 中保留此 URL 格式,但是 HybridUrlCodingStrategy 已被删除。在 wicket 1.5 中,页面安装为:

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

传统 URL 的结果如下:

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

我读到我们应该使用 MountedMapper 类,但是查看 Wicket 1.5 示例、API 文档和源代码,我仍然不清楚如何使用 MountedMapper 获得与 HybridUrlCodingStrategy 相同的行为。

有谁知道该怎么做?

We have an existing Java Wicket 1.4 application which uses the HybridUrlCodingStrategy extensively:

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

This results in our URL's looking like:

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. In wicket 1.5, pages are mounted as:

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

Which results in traditional URLs like:

http://host/myurl?paramName1=paramValue2¶mName2=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.

Does anyone know how to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

聊慰 2024-11-17 17:01:43

也许是这样的:

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

会起作用吗?当然,您必须手动指定参数,这可能需要更多工作。
MountedMapper 类 javadoc 解释了如何使用参数。

我能想到的另一个选择是(注意:这是未经测试的):

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());

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.

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());
对你再特殊 2024-11-17 17:01:43

无需自定义 IPageParametersEncoder。

使用 mountPage("/myurl/paramName1/${paramValue1}/paramName2/${paramValue2}", MyPage.class) 时,URL 将类似于 1.4 中的内容,但可以通过 StringValue value1 =parameters.get("paramValue1" 来访问值)。 value2 类似。

根据提取值,mountPage("/myurl/${paramValue1}/${paramValue2}", MyPage.class) 是相同的,只是将使用更短的 URL。

它还支持可选参数 - #{optionalValue3}。

No need of custom 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"). Similar for value2.

With mountPage("/myurl/${paramValue1}/${paramValue2}", MyPage.class) is the same according to extracting the values, just shorter URL will be used.

It also supports optional paramaters - #{optionalValue3}.

人疚 2024-11-17 17:01:43

注意:Wicket 1.5.2 中添加了一个新类,以向后兼容 1.4 样式 URL 编码。它称为 UrlPathPageParametersEncoder - 如果您要将 wicket 1.4 应用程序迁移到 1.5,并且您有以下样式的可书签页面链接,请使用它:

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

从 1.4 迁移时我们遇到了完全相同的问题至 1.5。任何已经运行一段时间的 1.4 应用程序都可能有一组从网络上的外部站点指向它的链接。您确实希望应用程序的 Wicket 1.5 版本能够处理这些现有的混合链接而不生成错误。

迁移到 1.5 时,如果没有 1.4 兼容的 IPageParametersEncoder 实现,如果您想避免对读取参数的每个单独 Page 类进行更改,则需要在每次装载中包含完整的参数规范。下面的实现意味着不再需要。只需按照上面 livid 的建议安装页面即可。

我将此 .java 文件作为补丁提交给 Wicket 开发人员 - 他们将来可能会将其包含在 Wicket 中,以便轻松为其他 1.4 迁移器实现向后兼容的 URL 参数编码。

我采用了上面 luniv 的示例代码,并做了一些小的更改以使其编译/工作。以下内容应作为参数编码器,在 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;
    }
}

NOTE: A new class has been added to Wicket 1.5.2 for backwards compatibility with 1.4 style URL encoding. 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:

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

We had the exact same issue when migrating from 1.4 to 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. You really want the Wicket 1.5 version of your app to be able to handle these existing hybrid links without generating an error.

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. The implementation below means that is no longer necessary. Just mount the page as livid suggests above.

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.

I took luniv's sample code above and made a few small changes to get it compiling/working. The following should work as a parameter encoder to provide 1.4.x style parameter encoding in 1.5.

    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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文