Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make \n work in <h:inputTextarea>

Tags:

java

jsf

I am using \n in my java bean and output of the variable in console is displayed correctly. Whereas while fetching this value from bean to JSF \n seems not working....... can any one suggest me how can i make \n work in of JSF.

like image 496
Akshatha Avatar asked Mar 30 '12 11:03

Akshatha


3 Answers

The simplest way would be to apply CSS white-space: pre on the parent element containing the text of which you would like to preserve newline \n characters. Given this CSS style class:

.preformatted {
    white-space: pre;
}

You could apply this as follows:

<div class="preformatted">#{bean.text}</div>

or

<h:panelGroup layout="block" styleClass="preformatted">#{bean.text}</h:panelGroup>

or

<h:outputText value="#{bean.text}" styleClass="preformatted" />

etc.

This style property is by the way also exactly what the <textarea> element is by default using. You could also make use of it and make it uneditable by setting disabled="true" or readonly="true".

<h:inputTextarea value="#{bean.text}" disabled="true" />

You can of course also replace all occurrences of \n by the HTML <br/> element. This way you can display it in an element which does not use white-space: pre and/or is not a <textarea> element. One of the ways is using fn:replace().

<h:outputText value="#{fn:replace(bean.text,'\\n','&lt;br/&gt;')}" escape="false" />

This is IMO only uglier than white-space: pre.

like image 162
BalusC Avatar answered Oct 16 '22 13:10

BalusC


You should replace all \n with <br/> before you send the value to your <h:inputTextarea>.

Html uses <br/> for line break and not the \n like java.

Also, you should add escape="false" to your <h:outputText (almost sure...).

like image 33
Daniel Avatar answered Oct 16 '22 12:10

Daniel


Replace all occurrences of \n with </br> before displaying it.

like image 2
Tom Avatar answered Oct 16 '22 13:10

Tom