如何将 HTML 代码添加到 JSF FacesMessage [英] How do I add HTML code to JSF FacesMessage
问题描述
默认情况下,每个 JSF FacesMessage
都显示在一行中.我想在消息本身中添加一个 HTML 换行符 <br/>
,以便整齐地显示消息.我试过如下
By default, every JSF FacesMessage
is presented in a single row. I would like to add a HTML line break <br />
to the message itself, so that the message is shown neatly. I tried it like below
message = new FacesMessage("test<br/>test");
然而,它被 JSF 转义并显示为文字文本.如何将 HTML 代码添加到 FacesMessage
而不被转义?
However, it got escaped by JSF and is shown as literal text. How can I add HTML code to a FacesMessage
without it getting escaped?
推荐答案
理论上,您需要 h:messages
组件的 escape
属性,例如 h:outputText
有.您不是唯一想要这个的人,以前经常要求这样做,但根据 JSF 人员的说法,这是一个 WONTFIX.
In theory, you want an escape
attribute for the h:messages
component like the h:outputText
has. You're not the only one who wants this, this is requested before more than often, but it's a WONTFIX according the JSF guys.
您有多种选择:
使用
并相应地应用 CSS(最简单).
Use
<br>
and apply CSS accordingly (easiest).
#messages td { white-space: pre; }
创建一个自定义渲染器 扩展了 MessageRenderer
(有点难,但如果你想覆盖更多的 HTML 而不仅仅是换行符,那就太好了).
Create a custom renderer which extends MessageRenderer
(bit harder, but nice if you want to cover more HTML than only linebreaks).
自己在 bean 中的某个 List
中收集消息并使用 <t:dataList>
,或者当您已经在使用 Facelets 而不是 JSP 时,使用 <ui:repeat>代码>.这样您就可以使用
<h:outputText escape="false">
来显示各个消息.
Gather the messages yourself in some List
in a bean and display them using <t:dataList>
, or when you're already using Facelets instead of JSP, using <ui:repeat>
. This way you can use <h:outputText escape="false">
to display the individual messages.
或者,当您已经使用 JSF 2.0 时,只需遍历 FacesContext#getMessageList()
自己.每个项目为您提供一个 FacesMessage
back 这反过来又提供了几个 getter.然后,您可以在 <h:outputText escape"false"/>
中显示摘要.
Or, when you're already on JSF 2.0, just iterate over FacesContext#getMessageList()
yourself. Each item gives you a FacesMessage
back which in turn offers several getters. You could then display the summary in a <h:outputText escape"false" />
.
<ul>
<ui:repeat value="#{facesContext.messageList}" var="facesMessage">
<li>
<h:outputText value="#{facesMessage.summary}" escape="false" />
</li>
</ui:repeat>
</ul>
或者,当您使用 JSF 实用程序库 OmniFaces 时,请使用其 <o:messages>
组件 代替它支持 escape
属性.
Or, when you're using JSF utility library OmniFaces, use its <o:messages>
component instead which has support for escape
attribute.
<o:messages escape="false" />
这篇关于如何将 HTML 代码添加到 JSF FacesMessage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!