WARNING: This blog entry was imported from my old blog on blogs.sun.com (which used different blogging software), so formatting and links may not be correct.
I first wrote the following at the end of my
previous blog entry a couple of minutes ago. However, since
that was a long blog on JavaScript, I figured those of you not interested in JavaScript
would miss it, so I decided to move the following into this separate blog entry instead.
Essentially (read the older blog entry if you're interested in the details)
the Output Text component has an Escape
property you can turn off
(it's on by default) which causes < to be emitted as < in the HTML.
You can use this to passthrough ANY HTML to your page. Please make sure
that it's valid XHTML though. This can be useful if you have some data on
the server you want to format nicely for the user; for example, you can generate
a table this way. Bind your output text to #{Page1.tableHtml}
and then put this in your page bean:
public String getTableHtml() {
StringBuffer sb = new StringBuffer();
sb.append("<table>"); // Add \n if you want resulting HTML to be more readable
for (int i = 0, n = result.size(); i < n; i++) {
sb.append("<tr>");
// Render your cells for result object i here
Foo foo = (Foo)result.get(i);
sb.append("<td>"); sb.append(foo.getName()); sb.append("</td>");
sb.append("<td>"); sb.append(foo.getAddress()); sb.append("</td>");
sb.append("</tr>");
}
sb.append("</table>");
return sb.toString();
}
The code is hopefully pretty self explanatory but the idea here is that on the server you have some list of objects you want to display, and for some reason, you can't use the preferred method of showing this in a JSF data table.
Now you can dynamically create the HTML right into your page from the server-side.
You can obviously go way overboard with this facility. If you want to be
really extreme, each one of your pages can have just a single Output Text component,
bound to a property, and then you compute the entire page HTML contents in
your page bean. But don't do that - in addition to the reduced designtime
help (WYSIWYG) you'll miss out on all the automation in generating HTML JSF
is providing for you. Over time the HTML emitted by the JSF components will
get more complicated and advanced. And JSF components have an abstraction where
the "rendering" is separate from the component, so your pages can render both
to HTML and to other display and interaction technologies such as those on
mobile devices. The more you tie yourself to HTML the harder that transition
will be.
hi and thanks tor it was very useful.
ReplyDeleteHi,
ReplyDeleteI've data in a mySQL table with new line characters and html tags. I want to display it at run time in the browser as html (using Sun Java Creator 2)
I created formattedoutput field in a page,bind it to a requestBean's String field , and attached a style sheet property for pre tag.
"white-space: pre;"
If the content is more than the width of the formattedoutput field it's not wrapping in firefox. but in IE6 it wraps., but it does not show any new lines. it puts everything into one line.
Any help would be appreciated
Take a look at this - it might help.
ReplyDeletehttp://www.cs.tut.fi/~jkorpela/html/nobr.html
still not working. Issue is not about the word wrap , it's about the line breaks.. it simply ignores the line break. for ex.
ReplyDeleteThis is a first line
This is a fourth line.
It ignores the newlines..
Then I don't know. IE is known to have lots of issues in this area -- google for tricks around line breaks and workarounds. There are many sites with IE workarounds.
ReplyDeleteIs this blog is created by Creator? In my comments I'd a blank line from first line to fourth line but it shows up as one paragraph., But it shows up your blog entry properly with line breaks and new paragraphs.. When you blog , do you put those HTML tags for line breaks and new paragraph? Please let me know.
ReplyDeleteNo, the blog is using the standard blogging software on blogs.sun.com -- Roller. You can enter linebreaks etc. but you must use HTML. In other words, you must put a <br/> tag for each newline, or place <p> paragraph tags around each paragraph. You can insert those in your generated HTML bound to your output text too.
ReplyDeleteThank you for your time. Won't bother you anymore. Your blog is cool and very useful.
ReplyDelete-- David