如何将HTML代码添加到JSF FacesMessage

时间:2022-05-27 20:03:02

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

默认情况下,每个JSF FacesMessage都显示在一行中。我想在消息本身添加一个HTML换行符,以便整齐地显示消息。我尝试过如下

message = new FacesMessage("test<br/>test");

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?

但是,它被JSF转义并显示为文字文本。如何在没有转义的情况下将HTML代码添加到FacesMessage?

4 个解决方案

#1


26  

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.

理论上,你想要一个h:messages组件的转义属性,就像h:outputText一样。你不是唯一一个想要这个的人,这是经常要求的,但是根据JSF的说法,这是一个WONTFIX。

You have several options:

你有几个选择:

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

    使用\ n而不是
    并相应地应用CSS(最简单)。

    #messages td { white-space: pre; }
    
  2. Create a custom renderer which extends MessageRenderer (bit harder, but nice if you want to cover more HTML than only linebreaks).

    创建一个扩展MessageRenderer的自定义渲染器(更难,但如果你想要覆盖更多的HTML而不仅仅是换行符,那就太好了)。

  3. 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.

    在bean的某些List中自己收集消息并使用 显示它们,或者当您已经使用Facelets而不是JSP时,使用 。这样您就可以使用 来显示各个消息。

  4. 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" />.

    或者,当您已经使用JSF 2.0时,只需自己迭代FacesContext#getMessageList()。每个项目都会为您提供一个FacesMessage,后者可以提供几个getter。然后,您可以在 中显示摘要。

    <ul>
        <ui:repeat value="#{facesContext.messageList}" var="facesMessage">
            <li>
                <h:outputText value="#{facesMessage.summary}" escape="false" />
            </li>
        </ui:repeat>
    </ul>
    
  5. Or, when you're using JSF utility library OmniFaces, use its <o:messages> component instead which has support for escape attribute.

    或者,当您使用JSF实用程序库OmniFaces时,请使用其支持escape属性的 组件。

    <o:messages escape="false" />
    

#2


3  

Primefaces 5.3 support HTML on FacesMessages, just setting escape="false" at messages component:

Primefaces 5.3支持FacesMessages上的HTML,只需在messages组件中设置escape =“false”:

<p:messages escape="false"/>

The p:growl supports this too btw.

p:咆哮也支持这个btw。

#3


2  

I followed this blog.

我关注了这个博客。

StringBuilder sb = new StringBuilder("<html><body>");
sb.append("<p>A list of messages are:</p>");
for(String str : listMessages){
      sb.append("Message: ").append(str).append("<br/>");
}  
sb.append("</body></html>");

FacesMessage message = new FacesMessage(sb.toString());
message.setSeverity(FacesMessage.SEVERITY_INFO);
FacesContext.getCurrentInstance().addMessage("", message);

The key is not to miss the <html> and <body> tags and closing them properly like a valid HTML. Otherwise HTML tags appear as text on the dialog.

关键是不要错过和标签,并像有效的HTML一样正确关闭它们。否则HTML标记在对话框中显示为文本。

#4


0  

Use a CSS class to format the message, as BalusC told you in your previous question:

使用CSS类来格式化消息,正如BalusC在您之前的问题中告诉您的那样:

Showing the JSF Error Messages

显示JSF错误消息

#1


26  

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.

理论上,你想要一个h:messages组件的转义属性,就像h:outputText一样。你不是唯一一个想要这个的人,这是经常要求的,但是根据JSF的说法,这是一个WONTFIX。

You have several options:

你有几个选择:

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

    使用\ n而不是
    并相应地应用CSS(最简单)。

    #messages td { white-space: pre; }
    
  2. Create a custom renderer which extends MessageRenderer (bit harder, but nice if you want to cover more HTML than only linebreaks).

    创建一个扩展MessageRenderer的自定义渲染器(更难,但如果你想要覆盖更多的HTML而不仅仅是换行符,那就太好了)。

  3. 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.

    在bean的某些List中自己收集消息并使用 显示它们,或者当您已经使用Facelets而不是JSP时,使用 。这样您就可以使用 来显示各个消息。

  4. 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" />.

    或者,当您已经使用JSF 2.0时,只需自己迭代FacesContext#getMessageList()。每个项目都会为您提供一个FacesMessage,后者可以提供几个getter。然后,您可以在 中显示摘要。

    <ul>
        <ui:repeat value="#{facesContext.messageList}" var="facesMessage">
            <li>
                <h:outputText value="#{facesMessage.summary}" escape="false" />
            </li>
        </ui:repeat>
    </ul>
    
  5. Or, when you're using JSF utility library OmniFaces, use its <o:messages> component instead which has support for escape attribute.

    或者,当您使用JSF实用程序库OmniFaces时,请使用其支持escape属性的 组件。

    <o:messages escape="false" />
    

#2


3  

Primefaces 5.3 support HTML on FacesMessages, just setting escape="false" at messages component:

Primefaces 5.3支持FacesMessages上的HTML,只需在messages组件中设置escape =“false”:

<p:messages escape="false"/>

The p:growl supports this too btw.

p:咆哮也支持这个btw。

#3


2  

I followed this blog.

我关注了这个博客。

StringBuilder sb = new StringBuilder("<html><body>");
sb.append("<p>A list of messages are:</p>");
for(String str : listMessages){
      sb.append("Message: ").append(str).append("<br/>");
}  
sb.append("</body></html>");

FacesMessage message = new FacesMessage(sb.toString());
message.setSeverity(FacesMessage.SEVERITY_INFO);
FacesContext.getCurrentInstance().addMessage("", message);

The key is not to miss the <html> and <body> tags and closing them properly like a valid HTML. Otherwise HTML tags appear as text on the dialog.

关键是不要错过和标签,并像有效的HTML一样正确关闭它们。否则HTML标记在对话框中显示为文本。

#4


0  

Use a CSS class to format the message, as BalusC told you in your previous question:

使用CSS类来格式化消息,正如BalusC在您之前的问题中告诉您的那样:

Showing the JSF Error Messages

显示JSF错误消息