以编程方式获取 cc:interface 定义

Get programmatically cc:interface definition

在这个例子中:

<composite:interface>
    <composite:attribute name="value" required="false" />
    <composite:editableValueHolder name="txtText"/>
</composite:interface>

<composite:implementation>
    <h:inputText id="txtText" value="#{cc.attrs.value}" />
</composite:implementation>

我想检索已设置为 editableValueHolder 的内容,就像我对属性所做的那样(例如 component.getAttributes().get("value")),但我没有找到这样做的方法

你可以通过managedBean得到你想要的。

一个简单的 POJO :

public class MyObject{

private String attribute;
private String editableValueHolder ;

//getter and setter
//...
}

您的 ManagedBean :

@ManagedBean
@ViewScoped
public class myManagedBean{

private MyObject selected;

//getter and setter
//...
}

您的网页:

<ui:composition
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:myLib="http://lib.dz/taglib">

<h:form>

<h:panelGrid columns="2">
    <h:outputText value="Attribute" />
    <h:inputText value="#{myManagedBean.attribute}" />
    <h:outputText value="EditableValueHolder " />
    <h:inputText value="#{myManagedBean.editableValueHolder }" />
</h:panelGrid>

</h:form>
</ui:composition>

此信息存储在 复合组件 BeanInfo 中,它可用作由 UIComponent.BEANINFO_KEY 键控的复合组件属性。

public static final String BEANINFO_KEY

The value of this constant is used as the key in the component attribute map, the value for which is a java.beans.BeanInfo implementation describing the composite component. This BeanInfo is known as the composite component BeanInfo.

<cc:editableValueHolder> 创建一个 EditableValueHolderAttachedObjectTarget instance in the List<AttachedObjectTarget> property of the composite component BeanInfo which is available by the key AttachedObjectTarget.ATTACHED_OBJECT_TARGETS_KEY

static final String ATTACHED_OBJECT_TARGETS_KEY

The key in the value set of the composite component BeanDescriptor, the value for which is a List<AttachedObjectTarget>.

总而言之,为了得到 txtText:

<cc:interface componentType="yourComposite">
    ...
    <cc:editableValueHolder name="txtText" />
</cc:interface>
<cc:implementation>
    <f:event type="postAddToView" listener="#{cc.init}" />
    <h:inputText id="txtText" ... />
    ...
</cc:implementation>
@FacesComponent("yourComposite")
public class YourComposite extends UINamingContainer {

    public void init() {
        BeanInfo info = (BeanInfo) getAttributes().get(UIComponent.BEANINFO_KEY);
        List<AttachedObjectTarget> targets = (List<AttachedObjectTarget>) info.getBeanDescriptor().getValue(AttachedObjectTarget.ATTACHED_OBJECT_TARGETS_KEY);

        for (AttachedObjectTarget target : targets) {
            if (target instanceof EditableValueHolderAttachedObjectTarget) {
                String name = target.getName();
                UIInput txtText = (UIInput) findComponent(name);
                // ...
            }
        }

    }

}

说起来,这都是多余的笨拙。更简单,more canonical approach 只是将子组件直接绑定到支持组件。

<cc:interface componentType="yourComposite">
    ...
    <cc:editableValueHolder name="txtText" />
</cc:interface>
<cc:implementation>
    <h:inputText binding="#{cc.txtText}" ... />
    ...
</cc:implementation>
@FacesComponent("yourComposite")
public class YourComposite extends UINamingContainer {

    private UIInput txtText; // +getter+setter

    // ...
}