是否建议对所有内容都使用 h:outputText ?
我是 JSF 新手(4 天前才开始学习),我对 h:outputText 的用法有点困惑。我知道这是一个简单的标签,但在我见过的大多数示例中,它用于输出非常简单(无需转义)的非 i18n 文本。例如(取自此处)
<h:outputText value="Transport" />
它可以被替换
Transport
所以,我想知道我是否遗漏了一些东西,或者我看到的大多数例子是否都过于复杂到了疯狂的地步。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用 JSF 2.x 和 Facelets 2.x 而不是 JSP,那么两者同样有效。更重要的是,Facelets 隐式地将内联内容包装在组件中,如
所示(换句话说,它将被转义!)。仅当您想要使用
escape="false"
禁用转义,或者想要分配id
、style
、onclick 时
等以编程方式,或者想使用转换器(通过converter
显式或通过forClass
隐式),那么您需要
。除非必要,我本人不会使用
。没有它,源代码会变得更好可读。您可以像这样#{bean.text}
那样在模板文本中内联 EL,而不是执行
。在 JSF 2.0 之前,在 JSP 和 Facelets 1.x 中,这是不可能的,因此
是强制性的。如果您的 IDE 对此发出警告,则很可能配置/考虑了 JSF 1.x。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 assignid
,style
,onclick
, etc programmatically, or would like to use a converter (either explicit viaconverter
or implicit viaforClass
), 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.您引用的示例是用 XHTML(即 XML)编写的。您想要放置的位置可能不允许使用独立的“传输”,因此您需要将其“转换”为有效的 xml。
IIrc 这就是所谓的 Facelets,并且是 JSF2 中的默认设置,而在 JSF1 中,表示代码可以使用 JSP 标记作为默认设置来完成,而 Facelets 是许多开发人员正在使用的替代方案。
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).
仅当您根据某些渲染条件渲染文本时才需要 h:outputText 标记。。
例如:
如果是简单的输出语句则不需要使用该标签;你可以使用: 运输
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