问题描述
我有以下情况:
<电话号码:selectBooleanCheckbox ID =couponAgreement>
< H:panelGroup中呈现=#{couponAgreement.valid!}>
是否有可能获得通过ID的组成部分?设置结合
将做到这一点,但是当我包括此多次在我的JSF页面,只有最后一个实例呈现。
我想是这样的:
< H:panelGroup中呈现=#{FN:!getComponentById('couponAgreement')}有效>
您可以使用 UIComponent#findComponent()
这一点。它会搜索相对于命名容器父。所以,如果你能保证这些部件有一个独特的命名容器的父(例如:< UI:重复>
,< F:子视图>
,< H:形式>
,等等),那么这样做:
< H:someInput ID =someInput... />
< H:someOutput ...渲染=#{component.findComponent('someInput')有效。}/>
至于结合
,你应该确保在结合
属性的值是专门绑组件本身,而不是跨多个组件共享。
所以,这是错误的,当它涉及在一个可重用的组件包括/ TAGFILE /复合材料:
< H:someInput绑定=#{} someInput... />
< H:someOutput ...渲染=#{} someInput.valid/>
相反,它绑定到一个唯一的密钥。让包括/ TAGFILE /复合材料需要 ID
参数/属性,然后使用 c为C:集>
以创建其中追加 ID
,这样就可以最终把它作为请求范围的地图键。
c为C:一套VAR =绑定值=_ binding_someInput#(编号)/>
< H:someInput ID =#(编号)绑定=#{requestScope [绑定]}... />
< H:someOutput ...渲染=#{requestScope [绑定] .valid}/>
要保持请求范围的清洁,可以考虑通过在创建请求范围哈希映射faces-config.xml中
:
<管Bean>
<描述>所有组件绑定的持有人< /描述>
<托管豆名称>元件及LT; /托管豆名称>
<管bean类>的java.util.HashMap< /管bean类>
<托管豆范围>请求< /托管豆范围>
< /管Bean>
< H:someInput ID =#(编号)绑定=#{组件[ID]}... />
< H:someOutput ...渲染=#{组件[ID] .valid}/>
在复合材料部件的情况下,有另一种方式。将它绑定为后盾的组成部分。
< CC:界面组件类型=someComposite>
...
< /立方厘米:接口>
< CC:实施>
< H:someInput绑定=#{} cc.someInput... />
< H:someOutput ...渲染=#{} cc.someInput.valid/>
< / CC:实施>
@FacesComponent(someComposite)
公共类SomeComposite扩展UINamingContainer { 私人UIInput someInput; // +吸气+二传手 // ...
}
在一个体面的复合材料设计往往你已经拥有或最终需要也无妨。
I have the below case:
<p:selectBooleanCheckbox id="couponAgreement">
<h:panelGroup rendered="#{!couponAgreement.valid}">
Is it possible to get an component by its id? Setting the binding
would do it, but when I include this multiple times in my JSF page, only the last instance is rendered.
I imagine something like this:
<h:panelGroup rendered="#{!fn:getComponentById('couponAgreement').valid}">
You can use UIComponent#findComponent()
for this. It will be searched relative to the naming container parent. So if you can guarantee that those components have an unique naming container parent (e.g. <ui:repeat>
, <f:subview>
, <h:form>
, etc), then do so:
<h:someInput id="someInput" ... />
<h:someOutput ... rendered="#{component.findComponent('someInput').valid}" />
As to binding
, you should just make sure that the value of the binding
attribute is exclusively tied to the component itself, and not shared across multiple components.
So, this is wrong when it concerns a component in a reusable include/tagfile/composite:
<h:someInput binding="#{someInput}" ... />
<h:someOutput ... rendered="#{someInput.valid}" />
Rather bind it to an unique key. Let the include/tagfile/composite require a id
param/attribute and then use <c:set>
to create a variable which appends the id
so that you can ultimately use it as key of request scope map.
<c:set var="binding" value="binding_someInput_#{id}" />
<h:someInput id="#{id}" binding="#{requestScope[binding]}" ... />
<h:someOutput ... rendered="#{requestScope[binding].valid}" />
To keep the request scope clean, consider creating a hash map in request scope via faces-config.xml
:
<managed-bean>
<description>Holder of all component bindings.</description>
<managed-bean-name>components</managed-bean-name>
<managed-bean-class>java.util.HashMap</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<h:someInput id="#{id}" binding="#{components[id]}" ... />
<h:someOutput ... rendered="#{components[id].valid}" />
In case of composite components, there's another way. Bind it to the backing component.
<cc:interface componentType="someComposite">
...
</cc:interface>
<cc:implementation>
<h:someInput binding="#{cc.someInput}" ... />
<h:someOutput ... rendered="#{cc.someInput.valid}" />
</cc:implementation>
@FacesComponent("someComposite")
public class SomeComposite extends UINamingContainer {
private UIInput someInput; // +getter+setter
// ...
}
In a decently designed composite you often already have or ultimately need it anyway.
这篇关于绑定失败时,组件包含在同一个页面中多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!