3

I have a web page that queries database dynamically to display data on the page, similar to database tool like Toad etc. (not even close of course :), example for illustration only).

The problem is data gets trimmed when displayed on the page. This is how I display data using JSF

<h:outputText value="#{record[columnIndex].toDisplayString()}" />

I believe it is about html rendering. What should I do? Write an html encoder? How? Help would be highly appreciated.

Bren
  • 2,148
  • 1
  • 27
  • 45
  • I believe that trimming data is okay in all cases. I think spacing and aligning should be done with use of html. – d1e Apr 13 '12 at 14:30

1 Answers1

16

The <h:outputText> doesn't trim the value at all.

Perhaps you're talking about whitespace like leading/trailing spaces, tabs, newlines, carriage returns, etc in the value, which have by default totally no meaning in HTML markup. It just becomes part of the HTML source code, but not the HTML presentation. Newlines, for example, are in HTML to be represented by the <br> element, not by the \n character.

If you'd like to preserve the whitespace in a HTML element node as it is in the HTML source code, then you need to set the parent HTML element's CSS white-space property to pre in order to preserve it. If you'd like to wrap lines in block elements, then use pre-wrap.

E.g.

<h:outputText ... styleClass="preformatted" />

with

.preformatted {
    white-space: pre-wrap;
}

An alternative is to convert the text to valid HTML markup yourself. E.g. replacing every occurrence of \n character by the <br/> string. You could use an EL function for this.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks a lot, i knew it was something to do with html, was looking for a component attribute like trim=false or something of that sort. Couldn't think of looking up for CSS. You are a life saviour mr. @BalusC – Bren Apr 13 '12 at 19:40