如何在 Wicket 1.5 中模仿 HybridUrlCodingStrategy?
我们有一个现有的 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¶mName2=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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许是这样的:
会起作用吗?当然,您必须手动指定参数,这可能需要更多工作。
MountedMapper 类 javadoc 解释了如何使用参数。
我能想到的另一个选择是(注意:这是未经测试的):
Maybe something like this:
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):
无需自定义 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}.
注意: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 风格的参数编码。
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.