Java 替代 ui:fragment 在 JSF 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3713468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 03:49:24  来源:igfitidea点击:

Alternative to ui:fragment in JSF

javajsf

提问by Giancarlo Corzo

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

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

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

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.

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

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

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

<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.

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

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.

另一种方法是使用<c:if test="#{condtion}">BUT c:if 从 JSF 树中删除元素,这不是您一直想要做的,特别是如果您使用 AJAX。

So you guys have another solutions?

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

采纳答案by BalusC

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

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

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.xmlfile). The renderedattribute is overlooked and missing in the tag file declaration (and also in the JSF 2.0 <ui:fragment>tag documentation), while it is reallypresent 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).

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

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>componentinstead:

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

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

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

它输出反正没事,如果你不指定idstylestyleClass和类似的属性,否则简单的<span>将被渲染。

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

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

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

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

然而,<f:verbatim>弃用的JSF 2.0(这反过来也对了一部纪录片的bug,在JSF 2.0<f:verbatim>标签文件没有提到折旧,但JSF 2.1<f:verbatim>标签文档一样)。