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.
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','<br/>')}" escape="false" />
This is IMO only uglier than white-space: pre
.
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...).
Replace all occurrences of \n
with </br>
before displaying it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With