I have a String
variable which contains carriage returns and new lines \\r\\n
.
text = \"Text1\\r\\nText2\\r\\nText3\";
The previous answers caused problems for me for large amounts of text, like extremely wide output (no text wrap), which style="white-space: pre-wrap"
did not fix; or it just displayed the <br/>
as text, not as a line ending.
In the end I used the h:inputTextarea
component to display the text and made it 'read only' which worked straight off without replacing the line breaks, nor having to use the extra styles or styleClasses. And, it is resizeable to suit the user's needs, which is a bonus.
For small amounts of text, I found the pre-wrap
option, as suggested by henko above, to work well.