Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display a line break with outputText?

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.  
like image 841
Thang Pham Avatar asked Jul 26 '10 17:07

Thang Pham


People also ask

How do you represent a line break in a string?

In Windows, a new line is denoted using “\r\n”, sometimes called a Carriage Return and Line Feed, or CRLF. Adding a new line in Java is as simple as including “\n” , “\r”, or “\r\n” at the end of our string.

How do you insert a break in line?

Press ALT+ENTER to insert the line break.

How do you break a line in Primefaces?

Add attribute escape="false" in h:outputText . If your value contains <br /> then you will have break line in text.

What is the code for line break?

The <br> HTML element produces a line break in text (carriage-return).


2 Answers

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> 
like image 131
BalusC Avatar answered Sep 27 '22 22:09

BalusC


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;     } 
like image 26
akhil Avatar answered Sep 27 '22 23:09

akhil