简体   繁体   中英

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

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','&lt;br/&gt;')}" 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...).

在显示之前将所有出现的\\n替换为</br>

When looking into text recorded in my database via a <h:inputTextarea> I found that special characters were being persisted.

Thus, after investigating what I thought was some dark art of persistence, I appreciated that the default display of the JSF component was in fact what was letting me down.

I shortly found that adding white-space: pre-wrap; to <p> on my stylesheet fixed this problem for my <h:outputText> tags which were being supplied with text from a JPA pojo.

In my case, I needed pre-wrap rather than pre because pre was wrapping by character, rather than by word.

Hope this helps someone!

I have solved it using the following method

  1. Declare an inputtextarea where a user can provide multi-line description. Before saving it to database, I have replaced new line to branch
String fpDescriptionCombined = fpDescription.replaceAll("(\r\n|\n)", "<br />");
  1. During showing in screen, i again replaced branch to new line
String finalStr = splitStr.replaceAll("<br />", "\n");
  1. In screen, I used above-mentioned CSS
.showLineBreak {
   white-space: pre;
 }
<h:outputText value="${templateListController.getFlowPointDescriptionForTab(eachFPType)}" style="display:block;width:860px;" styleClass="showLineBreak" />
  1. I had to do this because my description can be exported/imported via csv. So adding multi-line description will be treated as a new record if not handled properly

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM