Like many things having to do with JSF, it can be difficult to figure out how to render a line break (AKA new line) in the content of an outputText
tag. This has come up most frequently for me when I want to narrow the width of a table column by breaking the heading content into one or more lines. For example:
<h:column>
<h:outputText value="Very Wordy Table Column Heading" />
</h:column>
Your first (and last) thought might be to insert a line break tag (escaped, of course, so as not to break the XML formatting) in the content, like so:
<h:column>
<h:outputText value="Very Wordy<br />Table Column<br />Heading" />
</h:column>
But, this don't work:
Very Wordy<br
/>Table Column<br
/>Heading
Actually, this isn't far off. The key is to disable output escaping using the escape
attribute of the outputText
tag, like so:
<h:column>
<h:outputText value="Very Wordy<br />Table Column<br />Heading" escape="false" />
</h:column>
This works:
Very Wordy
Table Column
Heading
What's going on here? By default, the outputText
tag escapes HTML tags, which is great because it helps prevent injection attacks. What we're telling the outputText
tag to do is not do that. So, it follows that you shouldn't do this when dynamic, possibly unsafe content is being passed to the outputText
tag.
Hope this helps.
Leave a comment
37 comments
Thank you! Really helps me.
Ugly escape="false"....
You're welcome, Sergo!
Mike....
Thanks, it really helped me!
Great to hear, Alex. I appreciate it.
Mike....
I am using richfaces and
I tried it with
h:outputText value="#{someBean.someValue}" escape="false"
did not work for me
in bean someValue is "Very Wordy<br />Table Column<br />Heading"
it shows Very WordyTable ColumnHeading
@Athar Try replacing every "<" with "& lt;" and every ">" with "& gt;" (removing the spaces after the ampersands).
Mike....
Hello,
thanks for your post but it isn't working for me as well. I have a textarea where I can input some text and it will be saved in a text-field (@Lob) in database.
If I load the text into my textarea again the linefeeds are fine.
If I output the content in a the line feeds will be ignored.
So I did the following method (I didn't find any better solution in the hurry)
String content = selectedInterface.getKurzbeschreibung();
StringBuffer buffer = new StringBuffer();
for(int i = 0 ; i < temp.length(); i++) {
if((int)temp.charAt(i) == 10) {
buffer.append("<br />");
} else {
buffer.append(temp.charAt(i));
}
}
The result is A B D will be printed in one line.
I also put escape="false" to my Element.
Is there a way to use a textarea and let the frame disappear? I guess only with css.
Greetings,
Hauke
Sorry, I appended
& lt;br /& gt;
without the spaces and the result was
A B D
Sorry I can't edit my post, so between B and D was the br-tag 😃
Okay, I did is like this:
I put a textarea like this
readonly="true" style="background-image: none;border:none;resize: none;"
Hauke,
Thanks for the question. Textareas are different. Using normal newline/line-break characters works for me:
ArnBrnC
I hope this helps.
Mike....
Just use
and it works great.
What Hassan wrote, without the spacing:
Just use < code >< /code > and it works great.
Thanks for the input, Hassan.
Mike...
Thank you
thanks alot.. it works for me
i have a question for u michael.
i have a following code:
h:selectOneListbox
f:selectItem itemLabel="page1"
f:selectItem itemLabel="page2"
/h:selectOneListbox
here after selecting page1, it has to re-direct to page1.xhtml and after selecting page2 it has to re-direct to page2.xhtml.
i’m using spring webflow.
i dont want to use any java script code or java code.
Its pure jsf. How to do it??
reply soon
Hi Michael,
I have a question,.
i have the following Code in my Application
the problem am facing in, i am not able to Avoid line break. So, when i select the input text box the output text value breaks input 2 lines. Is there a way i can avoid line break?
Thanks.
Hi ,
Sorry forgot to paste the Code.
"h:outputText value="Application ID " /"
Thanks
Hey
Thank you, it was very straightforward and it worked.
Nice web btw, thank you again.
Hi ,
Unfortunately I'm not good English but I need urgent help please!
I have a problem:
how can I pass the contents of my textarea with line upheaval back.
Example, I have implemented a form with jsf and when I write in the textarea:
hello people
The weather is nice today.
I get: hello people, the weather is nice today.
index.xhtml
ouput.xhtml
Thks for the help .
This doesn't work with JSF 2.0 . I've tried this repeatedly. Either it's browser problem of the JSF version.
Tnks, a simple ideia that help me. =)
Thanks for providing your help, Very clear article here .
I have this code, so I want break line inside message?? this <br /> doesnt' work 😦.. Please can you help me?
<p:confirm header="Pošalji izvješća" icon="ui-icon-alert" style="white-space:pre-wrap"
message="Odabirom opcije 'Pošalji izvješća' zaključavate unose za cijeli period.<br />
Više nećete moći mijenjati niti dodavati podatke i dokumente,a izvješće će biti isporučeno
this is all code
Sorry for comments. It won't put it before :S