Solved

Display formatted output in JSF

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
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.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now