JSF 中 ui:fragment 的替代方案 [英] Alternative to ui:fragment in JSF

查看:18
本文介绍了JSF 中 ui:fragment 的替代方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种支持的方式来渲染 JSF 中的一段代码,我通常使用这种方法:

I'm searching a supported way to render a section of code in JSF, I usually use this approach:

<ui:fragment rendered="#{condition}">
   <h:outputText value="text 1"/>
   <h:outputText value="text 2"/>
   <h:outputText value="text 3"/>
</ui:fragment>

由于 ui:fragment 不支持渲染大部分 IDE(如 netbeans 将其标记为错误但它可以工作,因为在 JSF 中参数是继承的.

Since ui:fragment doesn't support rendered most of IDE (like netbeans mark it as error BUT it works because in JSF parameters are inherited.

解决这个问题的一种方法是使用另一种结构(例如,如果您使用 SEAM),您可以使用

One way to solve this is to use another structure (for example if you use SEAM) you can use

<s:div rendered="#{condition}">
   ....
</s:div>

另一种方法是像这样在所有内部内容中设置渲染:

Another way is to set the rendered in all inner content like this:

<h:outputText value="text 1" rendered="#{condition}"/>
<h:outputText value="text 2" rendered="#{condition}"/>
<h:outputText value="text 3" rendered="#{condition}"/>

但我不喜欢这种方式,因为你必须将渲染添加到每个元素中.

But I don't like this way because you have to add the rendered to each element.

另一种方法是使用 <c:if test="#{condtion}"> 但是 c:if 从 JSF 树中删除元素,这不是您一直想要的尤其是如果你使用 AJAX.

Another way would be to use <c:if test="#{condtion}"> BUT c:if remove the elements from the JSF tree and that not what you always want to do especially if you use AJAX.

所以你们有其他解决方案吗?

So you guys have another solutions?

推荐答案

由于 ui:fragment 不支持渲染的大部分 IDE(如 Netbeans 将其标记为错误但它可以工作,因为在 JSF 中参数是继承的)

Since ui:fragment doesn't support rendered most of IDE (like Netbeans mark it as error BUT it works because in JSF parameters are inherited)

这实际上是 JSF 2.0 Facelets 标记文件声明中的一个错误(在 Mojarra 中,这是 com/sun/faces/metadata/taglib/ui.taglib.xml 文件).rendered 属性在标记文件声明中被忽略和缺失(也在 JSF 2.0 标签文档),而它真的存在在 UIComponent 中.IDE 验证基于标记文件声明,因此会产生误导性的验证错误.此问题已在 JSF 2.1 中修复,缺少的属性已添加到标记文件声明中(也在 JSF 2.1 标签文档).

This is actually a bug in JSF 2.0 Facelets tag file declaration (in Mojarra, that's the com/sun/faces/metadata/taglib/ui.taglib.xml file). The rendered attribute is overlooked and missing in the tag file declaration (and also in the JSF 2.0 <ui:fragment> tag documentation), while it is really present in the UIComponent. The IDE validation is based on the tag file declarations and hence it gives a misleading validation error. This issue is fixed in JSF 2.1, the missing attribute is added to the tag file declaration (and also in the JSF 2.1 <ui:fragment> tag documentation).

如果只是忽略 IDE 警告或升级到 JSF 2.1 都不是一种选择,那么您可以考虑使用 组件:

If either just ignoring the IDE warnings or upgrading to JSF 2.1 is not an option, then you can consider using the <h:panelGroup> component instead:

<h:panelGroup rendered="#{condition}">
   <h:outputText value="text 1"/>
   <h:outputText value="text 2"/>
   <h:outputText value="text 3"/>
</h:panelGroup>

如果您不指定 idstylestyleClass 和类似属性,它无论如何不会输出任何内容,否则是一个简单的 将被渲染.

It outputs nothing anyway if you don't specify the id, style, styleClass and like attributes, else a simple <span> will be rendered.

如果您拥有的只是普通的 HTML(即没有 JSF 组件),那么您还可以考虑使用 .

If all what you have is plain vanilla HTML (i.e. no JSF components), then you can also consider using <f:verbatim>.

<f:verbatim rendered="#{condition}">
   text 1
   text 2
   text 3
</f:verbatim>

然而,<f:verbatim> 在 JSF 2.0 中被弃用(顺便说一下,它也有一个文档错误,JSF 2.0 标签文档 没有提到弃用,但 JSF 2.1 标签文档 确实如此).

However, the <f:verbatim> is deprecated in JSF 2.0 (which in turn has also a documentary bug by the way, the JSF 2.0 <f:verbatim> tag documentation does not mention deprecation, but the JSF 2.1 <f:verbatim> tag documentation does).

这篇关于JSF 中 ui:fragment 的替代方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆