Is it suggested to use h:outputText for everything?
Asked Answered
G

3

22

I'm new to JSF (just started learning about it 4 days ago) and I'm a bit confused about the usage of h:outputText. I know that is a simple tag, but in most examples I've seen, it's used to output very simple (no need to escape), non-i18n text. For example (taken from here)

<h:outputText value="Transport" />

which could be replaced by

Transport 

So, I'm wondering if I'm missing something or if most of the examples I've seen are overcomplicated to the point of insanity.

Gosplan answered 4/4, 2011 at 14:24 Comment(0)
P
47

If you're using JSF 2.x with Facelets 2.x instead of JSP, then both are equally valid. Even more, Facelets implicitly wraps inline content in a component as represented by <h:outputText> (in other words, it will be escaped!).

Only whenever you'd like to disable escaping using escape="false", or would like to assign id, style, onclick, etc programmatically, or would like to use a converter (either explicit via converter or implicit via forClass), then you need <h:outputText>.

I myself don't use <h:outputText> whenever it is not necessary. Without it, the source code becomes better readable. You can just inline EL in template text like so #{bean.text} instead of doing <h:outputText value="#{bean.text}">. Before JSF 2.0, in JSP and Facelets 1.x, this was not possible and thus the <h:outputText> is mandatory. If your IDE gives warnings on this, it's most likely JSF 1.x configured/minded.

Pericline answered 4/4, 2011 at 15:36 Comment(5)
These are good news! Didn't know it. One more point for JSF. Thanks BalusC!Xyloid
Thanks Balus. I've been removing that tag and the app didn't fail, so I must be using facelets2 (as I'm not using JSP). Thanks for the info!Gosplan
You're welcome. Please note that the other answer is technically incorrect for Facelets.Pericline
@balusc A complete (or even partial) list of conventions like this could be really useful to make code manageable across a large team... Do you happen to know of one? I like the complete removal of all outputText unless necessary as well. It's much cleaner that way.Vermouth
@Lucas: Sorry, no such public list comes to mind. You'd have to manage your own in cooperation with the JSF lead/architect. By the way, you may find some "best practices" or "hidden features" in this article useful for your projects: balusc.blogspot.com/2011/09/communication-in-jsf-20.html Implicit output text is also mentioned over there.Pericline
M
2

The example you quote is written in XHTML - which is XML. A standalone 'Transport' may not be allowed at the position you want to put it in, so that you need to "transform" it into valid xml.

IIrc this what is called facelets and the default in JSF2, while in JSF1, the presentation code could be done with JSP tags as default and facelets was an alternative that many developers were using).

Meggie answered 4/4, 2011 at 14:54 Comment(1)
Gosh, you're right. Sorry for this extra question, but do you know if there's an alternative? Maybe to write the templates in groovy? It's just that the extra complexity is astonishing.Gosplan
P
0

h:outputText tag is required only if you are rendering the text based on some render condition. eg: <h:outputText value="Transport" rendered="#{myBean.displayText}"/>. If its a simple output statement then there is no need of using the tag; you could just use: Transport

Pinpoint answered 5/12, 2014 at 22:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.