aspose file tools*
The moose likes JSF and the fly likes HOw do I replace new lines with <BR> tags? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "HOw do I replace new lines with <BR> tags?" Watch "HOw do I replace new lines with <BR> tags?" New topic
Author

HOw do I replace new lines with <BR> tags?

Dave Alvarado
Ranch Hand

Joined: Jul 02, 2008
Posts: 436
Hi,

I"m using MyFaces 1.1.5 with Tomahawk 1.1.7. Does anyone know how I can replace carriage returns ("\n" characters) with
so that my text is output and properly formatted ? The <PRE> tag is not an option as it doesn't allow styling of the text within it. Currently I am wrapping the text in a span with a particular class and then using Javascript to find all instances of that class and replace the HTML within it. A ridiculous hack, but there is surprisingly no help for this subject online.

Thanks, - Dave
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16250
    
  21

As a rule, I don't like to generate HTML in the code. Wherever possible, I prefer to keep the display stuff in the display and the logic stuff in the logic components. But there are times when this isn't practical. For example, I have an app which allows people to edit, store, and display formatted text on web pages. It uses a RichFaces HTML editor control, so the raw data is in HTML form (which is still better than hard-coding HTML in java code).

To display that kind of stuff, you can use the <h:outputText escape="false" value="...."/> construct. If your text contains end-of-line characters, you can use a string search-and-replace function to substitute "<br/>" tags and output the results using outputText.

Earlier versions of JSF did not handle intermixing raw HTML and JSF tags very well. It's much better these days, but you'll still end up with tidier, more portable displays if you minimize the HTML and use the JSF equivalents.


Customer surveys are for companies who didn't pay proper attention to begin with.
Tom mmeke
Greenhorn

Joined: Jul 19, 2011
Posts: 10

If you're using a backing bean to get your text, just use string replacement to replace "\n" with "<br />".


Then use escape = false in your outputText field.

* This answer might come a bit late for the OP. But google brought me here, so it might help someone else. *
Gustavo Guevara
Greenhorn

Joined: Sep 06, 2012
Posts: 1
Tom mmeke wrote:If you're using a backing bean to get your text, just use string replacement to replace "\n" with "<br />".


Then use escape = false in your outputText field.

* This answer might come a bit late for the OP. But google brought me here, so it might help someone else. *


It helped me out, thank you. I did however end up with a bit of a doubt. What do you mean by "entity" on this line?



 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HOw do I replace new lines with <BR> tags?