以编程方式获取cc:接口定义

时间:2016-06-18 19:25:22

标签: jsf interface composite-component

在这个例子中:

<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")),但我找不到这样做的方法

2 个答案:

答案 0 :(得分:2)

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

  

public static final String BEANINFO_KEY

     

此常量的值用作组件属性映射中的键,其值是描述复合组件的java.beans.BeanInfo实现。此BeanInfo称为复合组件BeanInfo

<cc:editableValueHolder>复合组件BeanInfo EditableValueHolderAttachedObjectTarget属性中创建一个List<AttachedObjectTarget>实例,该实例可由密钥AttachedObjectTarget.ATTACHED_OBJECT_TARGETS_KEY使用。

  

static final String ATTACHED_OBJECT_TARGETS_KEY

     

复合组件BeanDescriptor 的值集中的键,其值为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

    // ...
}

答案 1 :(得分:-2)

您可以通过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>