Solved

Display formatted output in JSF

Posted on 2009-04-02
2
2,563 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
scoresSpecial  challenge 13 52
powerN  challenge 3 63
replace a word with other 1 44
sql import cannot be resolved jsp 3 19
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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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.

776 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