Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2726
  • Last Modified:

Display formatted output in JSF

I have a web application that uses jsf.  My form allows a user to enter paragraphs into an h:inputTextarea component, and they can enter carriage returns and it saves the formatting correctly into the database.  After saving and closing the form, they can open that form back up in read mode to see what they had written.  I am using h:outputText to display their paragraphs, but outputText doesn't display the carriage returns.

When the user opens the form in edit mode, the carriage returns correctly display in the h:inputTextarea.  But what can I use in JSF to preserve the format of their data while in read mode?
<h:inputTextarea style="width:700px;height:100px;" value="#{paragraph.bodyText}" rendered="#{bean.mode='Edit'}"/>
 
<h:outputText value="#{paragraph.bodyText}" rendered="#{bean.mode='Read'}"/>

Open in new window

0
chuckie474
Asked:
chuckie474
2 Solutions
 
KadhiravanCommented:
You can use h:outputFormat to save the formatting. This will render the exact formatting in read mode.

Refer the following link on how to use the h:outputFormat.

http://www.jsftoolbox.com/documentation/help/12-TagReference/html/h_outputFormat.html
0
 
chuckie474Author Commented:
That didn't work for me - it still showed all the lines running together.

But I ended up figuring out a solution:

I have a backing bean that I used to display the text, and I called a convertClobToString method to get the string from the Oracle clob.  In the conversion, the clob stores its carriage returns as "\n".  JSF doesn't know how to translate \n in an h:outputText, so I have to convert the \n to and html <br>.  So now I have two properties on my backing bean:

public String bodyText;
public String bodyTextDisp;

When I pull the data from the database, I store the values into the backing bean.

bodyText = convertClobToString(oracleClob);
bodyTextDisp = bodyText.replaceAll("[\n]", "<br>");

So now, in my JSF, I can use the bodyText field for the inputTextArea and the bodyTextDisp field for the outputText.  Because the bodyTextDisp now has <br> tags, I have to add an escape="false" tag to render the html properly.

<h:inputTextarea style="width:700px;height:100px;" value="#{paragraph.bodyText}" rendered="#{bean.mode='Edit'}"/>
 
<h:outputText escape="false" value="#{paragraph.bodyTextDisp}" rendered="#{bean.mode='Read'}"/>

Open in new window

0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now