如何添加HTML代码到JSF FacesMessage [英] How do I add HTML code to JSF FacesMessage

查看:151
本文介绍了如何添加HTML代码到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?

推荐答案

p>理论上,你想要 $ $ $ 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.

您有几个选项:


  1. 使用 \\\
    而不是< br> 并相应地应用CSS(最简单的)。

  1. Use \n instead of <br> and apply CSS accordingly (easiest).

#messages td { white-space: pre; }


  • 创建一个自定义渲染器,它扩展了 MessageRenderer (更难,但很好

  • Create a custom renderer which extends MessageRenderer (bit harder, but nice if you want to cover more HTML than only linebreaks).

    列表中收集消息在一个bean中,并使用 < 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 返回,反过来又提供了几个吸烟者。然后,您可以在< h:outputText escapefalse/> 中显示摘要。

    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 属性。

    <o:messages escape="false" />
    


  • 这篇关于如何添加HTML代码到JSF FacesMessage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆