绑定失败时,组件包含在同一个页面中多次 [英] Binding fails when a component is included multiple times in same page

查看:149
本文介绍了绑定失败时,组件包含在同一个页面中多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情况:

 <电话号码:selectBooleanCheckbox ID =couponAgreement>
< H:panelGroup中呈现=#{couponAgreement.valid!}>

是否有可能获得通过ID的组成部分?设置结合将做到这一点,但是当我包括此多次在我的JSF页面,只有最后一个实例呈现。

我想是这样的:

 < H:panelGroup中呈现=#{FN:!getComponentById('couponAgreement')}有效>


解决方案

您可以使用<一个href=\"http://docs.oracle.com/javaee/7/api/javax/faces/component/UIComponent.html#findComponent-java.lang.String-\"相对=nofollow> UIComponent#findComponent() 这一点。它会搜索相对于命名容器父。所以,如果你能保证这些部件有一个独特的命名容器的父(例如:&LT; UI:重复&GT; &LT; F:子视图&GT; &LT; H:形式&GT; ,等等),那么这样做:

 &LT; H:someInput ID =someInput... /&GT;
&LT; H:someOutput ...渲染=#{component.findComponent('someInput')有效。}/&GT;

至于结合,你应该确保在结合属性的值是专门绑组件本身,而不是跨多个组件共享。

所以,这是错误的,当它涉及在一个可重用的组件包括/ TAGFILE /复合材料:

 &LT; H:someInput绑定=#{} someInput... /&GT;
&LT; H:someOutput ...渲染=#{} someInput.valid/&GT;

相反,它绑定到一个唯一的密钥。让包括/ TAGFILE /复合材料需要 ID 参数/属性,然后使用 c为C:集&gt; 以创建其中追加 ID ,这样就可以最终把它作为请求范围的地图键。

变量

  c为C:一套VAR =绑定值=_ binding_someInput#(编号)/&GT;
&LT; H:someInput ID =#(编号)绑定=#{requestScope [绑定]}... /&GT;
&LT; H:someOutput ...渲染=#{requestScope [绑定] .valid}/&GT;

要保持请求范围的清洁,可以考虑通过在创建请求范围哈希映射faces-config.xml中

 &LT;管Bean&GT;
    &LT;描述&GT;所有组件绑定的持有人&LT; /描述&GT;
    &LT;托管豆名称&gt;元件及LT; /托管豆名称&gt;
    &LT;管bean类&GT;的java.util.HashMap&LT; /管bean类&GT;
    &LT;托管豆范围&GT;请求&LT; /托管豆范围&GT;
&LT; /管Bean&GT;

 &LT; H:someInput ID =#(编号)绑定=#{组件[ID]}... /&GT;
&LT; H:someOutput ...渲染=#{组件[ID] .valid}/&GT;

在复合材料部件的情况下,有另一种方式。将它绑定为后盾的组成部分。

 &LT; CC:界面组件类型=someComposite&GT;
    ...
&LT; /立方厘米:接口&GT;
&LT; CC:实施&GT;
    &LT; H:someInput绑定=#{} cc.someInput... /&GT;
    &LT; H:someOutput ...渲染=#{} cc.someInput.valid/&GT;
&LT; / CC:实施&GT;

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

这篇关于绑定失败时,组件包含在同一个页面中多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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