使用标签生成部分格式化的文本
我在此处阅读了有关 wicket:message
的内容,但似乎无法让它完成我想要的一切。
假设我有一个包含
的 HTML 页面和一个包含 text=Blah blah: important point, foo bar
的属性文件。我想知道如何将文本的部分加粗(或对其应用任意CSS),以实现如下输出:
Blah blah:重要的一点,foo bar
请注意,这些实际上都不是动态的,所以如果可以避免的话,我不想在 Java 中做任何事情。
我尝试过使用类似以下内容的嵌套标签,但没有成功。
<wicket:message key="text">
<span class="bold"><wicket:message key="text2"/></span>
</wicket:message>
text=Blah blah: ${text2}, foo bar
text2=important point
这在 Wicket 中是否可能实现,而无需 1)从 Java 端注入格式化部分或 2)仅将文本拆分为(在本例中)三个不同的属性?
I've read about wicket:message
here, but can't seem to make it do everything I'd like.
Say I have a HTML page with <wicket:message key="text"/>
and a properties file containing text=Blah blah: important point, foo bar
. I'm wondering how to make part of the text bold (or apply arbitrary CSS to it), to achieve output like:
Blah blah: important point, foo bar
Note that none of this is actually dynamic, so I wouldn't want to do anything in Java, if that can be avoided.
I've tried nesting tags with something like the following, but no luck.
<wicket:message key="text">
<span class="bold"><wicket:message key="text2"/></span>
</wicket:message>
text=Blah blah: ${text2}, foo bar
text2=important point
Is this even possible in Wicket without 1) injecting the formatted part from Java side or 2) just splitting the text into (in this case) three different properties?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最简单的方法是将标签放入本地化文件中:
您还可以稍后使用 Label 和 ResourceModel 来替换它:
在您的模型 getObject() 或您的标签中:
或者,更好的是,尝试在标签中重用 Markdown 实现。
The easiest way is to put the tags inside your localization file:
You could also use a Label and a ResourceModel to replace it later:
And in your model getObject(), or in your Label:
Or, even better, try to reuse a Markdown implementation in your Label.
我已经成功地为我自己的应用程序做到了这一点,尽管使用了相当丑陋的技巧。我通过公开 WicketMessageResolver 的自定义版本来做到这一点。
可以尝试以下方法:
将 org.apache.wicket.markup.resolver.WicketMessageResolver 批量复制并粘贴到您自己的类中(例如 com.acme.CustomWicketMessageResolver)(黑客开始!)
在您的 CustomWicketMessageResolver 更改中
将
WicketTagIdentifier.registerWellKnownTagName( "message" );
更改为WicketTagIdentifier.registerWellKnownTagName( "msg" );
之类的内容。里面的
private void renderMessage(final MarkupStream markupStream, Final ComponentTag openTag, Final String key, Final String value)
,您会发现行getResponse().write( text );
。紧接着该行之前,您有机会修改“text”的值。在那里,我做了类似
text = MyLabelUtils.replaceWikiMarkup(text)
的操作,它对内容作者在我的应用程序中使用的一些类似 wiki 的标记语法进行后处理。例如,我使用此方法使用指向键的 ResourceModel 来获取标签:
propertyKey=I find the answer on [acronym SO].
并将其渲染为
I find the answer关于SO
。该方法处理 i18n 和所有有趣的东西。
当然,您可以根据需要扩展 wiki 语法(或任何类似的语法),使其变得简单或复杂。
请注意,您必须在所有标记中将
更改为我显然更喜欢一种更标准的方法来自定义内置wicket消息解析器的行为,但如果您像我一样在紧要关头需要此功能,那么现在就可以使用。
如果您需要更标准的内容,您可以在 Wicket 邮件列表上提出问题。非常好。
I've managed to do this for my own application, albeit with a rather ugly hack. I did it by exposing a customized version of WicketMessageResolver.
Here's what to try:
Wholesale copy and paste
org.apache.wicket.markup.resolver.WicketMessageResolver
into your own class (say com.acme.CustomWicketMessageResolver) (the hack begins!)Inside your CustomWicketMessageResolver change
WicketTagIdentifier.registerWellKnownTagName( "message" );
to something else likeWicketTagIdentifier.registerWellKnownTagName( "msg" );
.Inside of
private void renderMessage(final MarkupStream markupStream, final ComponentTag openTag, final String key, final String value)
, you'll find the linegetResponse().write( text );
.Immediately before that line you have the opportunity to screw around with the value of "text". There, I do something like
text = MyLabelUtils.replaceWikiMarkup(text)
which post-processes some wiki-like markup syntax used by the content authors for my application.For example, I use this method to take a Label using a ResourceModel pointing to the key:
propertyKey=I found the answer on [acronym SO].
and a render it as
I found the answer on <acronym title="Stack Overflow">SO</acronym>
.and that method handles i18n and all that fun stuff.
You can, of course, extend that wiki syntax (or anything similar) to be as simple or complex as you'd need.
Note that you'll have to change
<wicket:message key='foo'>
to<wicket:msg key='foo>
in all of your markup files (or at least in ones where you want this behaviour).I'd obviously prefer a more standard way to customize the behaviour of the built-inwicket message resolver, but if you need this functionality in a pinch, like I did, this will work for now.
If you need something more standard, you could raise the issue on the Wicket mailing list. It's pretty good.
从 Wicket 1.4 开始,您可以在 wicket:message 元素中嵌套组件。例如:
Then
和
Results in:
因此,也许,在没有组件的情况下嵌套
也可以工作。没有把握。来源: https://cwiki .apache.org/confluence/display/WICKET/Wicket%27s+XHTML+tags#Wicket%27sXHTMLtags-Elementwicket%3Amessage
Starting from Wicket 1.4 you can nest components within a wicket:message element. For example:
Then
and
Results in:
So maybe, nesting
<wicket:message>
s without a component could work as well. Not sure.Source: https://cwiki.apache.org/confluence/display/WICKET/Wicket%27s+XHTML+tags#Wicket%27sXHTMLtags-Elementwicket%3Amessage