在本例中:
<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")
)一样,但是我没有找到这样做的方法
发布于 2016-06-19 01:04:34
这些信息存储在复合组件BeanInfo中,该组件可以作为UIComponent.BEANINFO_KEY
键控的复合组件属性使用。
public static final String BEANINFO_KEY
此常量的值被用作组件属性映射中的键,其值是描述复合组件的java.beans.BeanInfo
实现。这个BeanInfo
被称为复合组件BeanInfo。
<cc:editableValueHolder>
在复合组件BeanInfo的List
属性中创建一个EditableValueHolderAttachedObjectTarget
实例,该组件由键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
// ...
}
发布于 2016-06-19 00:45:23
你可以通过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>
https://stackoverflow.com/questions/37900832
复制相似问题