Solved

Display formatted output in JSF

Posted on 2009-04-02
2
2,584 Views
Last Modified: 2013-11-24
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
Comment
Question by:chuckie474
2 Comments
 

Assisted Solution

by:Kadhiravan
Kadhiravan earned 50 total points
ID: 24059246
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
 

Accepted Solution

by:
chuckie474 earned 0 total points
ID: 24063398
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question