将链接动态添加到 Wicket 中的字符串中
我有一个字符串(用户消息),我想在其中找到(使用正则表达式)特殊代码并将其替换为链接。 (例如,@user
将替换为 user 事物。)如何在 Wicket 中完成此操作?我不问有关正则表达式的问题,这是以 Wicket 为中心的问题。
I have a string (user message) and I would like to find (using regexp) special codes in it and replace them with links. (For example, @user
will be replaced with <a href="wicket-url-to-user-profile">user</a>
thing.) How can it be done in the Wicket? I do not ask about regular expressions, it is Wicket-focused question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
事实上,Wicket对此有一个非常强大的功能。
假设这是带有“some.resource.key”键的本地化字符串:
然后将以下内容添加到 Wicket 模板中:
在 Wicket 页面(或面板)中,只需添加链接,就像将其添加到关卡中一样现在
,链接(您可以使用链接的任何实现或您喜欢的任何其他组件)将正确显示并嵌入您的本地化字符串中。
In fact, Wicket has a very powerful feature for this.
Let's say this is your localization string with the key "some.resource.key":
Then you add the following to your Wicket template:
In your Wicket page (or Panel), simply add your link as if you were adding it to the level where you put the wicket:message:
Now the link - you can use whatever implementation of link or any other component you like - is displayed correctly embedded in your localized string.
Wicket 有 IResponseFilter。这样您就可以对最终的 HTML 进行后处理。即,您可以搜索 ${anything} 并将其替换为“无论您想要什么”。
要创建漂亮的 URL,我建议您为特定域对象挂载特定页面,例如 Application#mountPage("/users/${user}", UsersPage.class),并使用以下命令创建 URL:RequestCycle.get()。 urlFor(UsersPage.class, pageParameters),其中“pageParameters”包含一个带有键“user”和值“您使用 RegEx 提取的内容”的条目。
Wicket has IResponseFilter. With this you can post-process the final HTML. I.e. you can search for ${anything} and replace it with 'whatever you want'.
To create nice looking URLs I suggest you to mount specific pages for the specific domain objects, e.g. Application#mountPage("/users/${user}", UsersPage.class), and create the urls with : RequestCycle.get().urlFor(UsersPage.class, pageParameters), where 'pageParameters' contains an entry with key 'user' and value 'whatever you extracted with RegEx'.