gpt4 book ai didi

jsf - 有条件地渲染复合组件中元素的属性

转载 作者:行者123 更新时间:2023-12-02 20:55:52 27 4
gpt4 key购买 nike

我有以下复合组件:

<?xml version="1.0" encoding="UTF-8"?>
<ui:component xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:fn="http://java.sun.com/jsp/jstl/functions"
xmlns:composite="http://java.sun.com/jsf/composite">

<composite:interface>
<composite:attribute required="true" name="field" />
<composite:attribute required="true" name="value" />
<composite:attribute required="false" name="size"/>
</composite:interface>

<composite:implementation>
...
<div class="wrapper">
<h:inputText value="#{cc.attrs.value}"
id="#{field.id}"
rendered="#{field.rendered}"
size="#{cc.attrs.size}">
</h:inputText>
<h:messages for="#{field.id}" styleClass="errorMessage"/>
</div>
...
</composite:implementation>
</ui:component>

问题是,当我使用此组件而不设置其 size 属性时,它仍然在 html 输入元素中呈现为 size=0

我想要的是仅在嵌套的 h:inputText 属性具有有效值(例如非空)时才渲染它。或者,如果没有显式覆盖嵌套元素的所有属性,我想公开它们。

这怎么可能?

最佳答案

您可以使用JSTL <c:if>有条件地构建 View 和 <f:attribute>单独指定属性:

<h:inputText ...>
<c:if test="#{not empty cc.attrs.size}">
<f:attribute name="size" value="#{cc.attrs.size}" />
</c:if>
</h:inputText>

另一种方法是为复合组件属性指定默认值:

<cc:attribute name="size" required="false" default="10" />

关于jsf - 有条件地渲染复合组件中元素的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12897202/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com