48

I need to render a line break using outputText so that I can utilize the rendered attributed. I tried

<h:outputText value="<br/>" escape="false" />

but it generated exception

The value of attribute "value" associated with an element type "null" must not contain the '<' character. 
Tiny
  • 27,221
  • 105
  • 339
  • 599
Thang Pham
  • 38,125
  • 75
  • 201
  • 285

3 Answers3

102

That's indeed not valid since Facelets because it's syntactically invalid in XML. You'd need to manually escape the XML special characters like <, > and so on.

<h:outputText value="&lt;br/&gt;" escape="false" />

You can however just emit the <br/> in template text without the need for a <h:outputText>.

<br/>

To render it conditionally, wrap it in for example a <ui:fragment>.

<ui:fragment rendered="#{bean.rendered}"><br /></ui:fragment>

A <h:panelGroup> is also valid as it doesn't emit anything to the HTML anyway.

<h:panelGroup rendered="#{bean.rendered}"><br /></h:panelGroup>
musicin3d
  • 1,028
  • 1
  • 12
  • 22
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    how would you do this when dynamically adding an `HtmlOutputText` in a managed bean? I tried `HtmlOutputText linebreak = new HtmlOutputText(); lineBreak.setValue("<br/>" and "
    ");` (obviously not at the same time like that) but neither worked, they simply outputted the text entered, not a break
    – Austin Aug 15 '12 at 20:11
  • 1
    @Austin: Apparently you forgot calling `setEscape(false)`. – BalusC Aug 15 '12 at 20:15
  • sure you don't need any new books? =) – Austin Aug 15 '12 at 20:35
  • Thank you BalusC! If your text with line-breaks comes from an EL expression, you need to use an **UN**-escaped line-break element. – jahroy Feb 13 '13 at 04:27
5

JSF PAGE

<h:outputText value="#{car.crg}" escape="false" style="white-space: pre-wrap;word-wrap: break-word; " />

escape should be false and write the bean Getter method as follows

 public String getCrg() {
         return crg.replace("<br/>", "&lt;br /&gt;");
        //return crg;
    }
display name
  • 4,165
  • 2
  • 27
  • 52
akhil
  • 69
  • 1
  • 1
0

You can try putting the "<br />" inside a resource bundle and then get the value from that resource bundle.

Jaime Garcia
  • 6,744
  • 7
  • 49
  • 61