有条件地显示多个元素(例如,取决于 bean 值的字段列表)的最佳策略是什么?
我想到的可能的解决方案:
- 江苏实验室
<c:if ... >
条款。从我 了解将 JSTL 与 JSF 结合使用是 气馁 - 使用“渲染” 大多数组件的属性。 不幸的是当我不得不处理 有了很多领域,它就变成了 笨拙地把它们放在每个人身上......
- 将元素放在容器中 并设置渲染属性 在容器上
选项 3 似乎是最明智的,但我不知道使用什么组件来包装这些字段。它需要是一个不影响布局的组件...
我可以使用 span 作为包装器并设置 CSS 可见属性,但字段仍会呈现,只是不可见。
有什么想法吗?
更新:
这是一些实际的布局代码。我都试过了 <h:panelGroup>
& <ui:fragment>
.使用这些标签中的任何一个都会将我的所有字段放在一个 <td>
中,我承认,这是有道理的,因为我在我的 panelGrid
中放置了一个顶级元素。 .
唯一符合我要求的是上面列表中的 #2。
<h:panelGrid columns="2">
<!-- fields if person -->
<ui:fragment rendered="#{createEntity.entityType eq 'fizica'}">
<h:outputLabel value="Prenume: " />
<h:inputText value="#{createEntity.person.firstName}" />
<h:outputLabel value="Nume familie: " />
<h:inputText value="#{createEntity.person.familyName}" />
<h:outputLabel value="CNP: " />
<h:inputText value="#{createEntity.person.cnp}" />
<h:outputLabel value="Date carte identitate: " />
<h:inputText value="#{createEntity.person.idCardInfo}" />
<h:outputLabel value="Cetatenie: " />
<h:inputText value="#{createEntity.person.country}" />
</ui:fragment>
<!-- fields for company -->
<ui:fragment rendered="#{createEntity.entityType eq 'juridica'}">
<h:outputLabel value="Denumire firma" />
<h:inputText value="#{createEntity.company.name}" />
<h:outputLabel value="CUI" />
<h:inputText value="#{createEntity.company.cui}" />
<h:outputLabel value="Registrul Comertului" />
<h:inputText value="#{createEntity.company.regCommerceNo}" />
</ui:fragment>
</h:panelGrid>
最佳答案
渲染是显示(或不显示)组件的推荐方式。
如果可以将它们组合在一起,那么设置容器的rendered属性就完全有效了。我会这样做,而不是在每个单独的组件上设置渲染属性。
唯一的小缺点是如果显示是动态的(您更改辅助 bean 值),并且您需要重新显示容器,则它不存在。您需要将容器包装在另一个容器中,然后重新渲染。
关于java - 多个字段的条件显示 - JSF 2.0/Primefaces,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5592248/