Primefaces 如何将JSF复合组件属性设置为ManagedBean属性?
所以我制作了一个复合组件FileAdder.xhtmlPrimefaces 如何将JSF复合组件属性设置为ManagedBean属性?,primefaces,attributes,jsf-2.2,managed-bean,composite-component,Primefaces,Attributes,Jsf 2.2,Managed Bean,Composite Component,所以我制作了一个复合组件FileAdder.xhtml <composite:interface> <composite:attribute name="type" value="#{editoriCompositeController.typeString}"/> </composite:interface> <composite:implementation> <h:form> <p:edit
<composite:interface>
<composite:attribute name="type" value="#{editoriCompositeController.typeString}"/>
</composite:interface>
<composite:implementation>
<h:form>
<p:editor id="editor" widgetVar="editorWidget" value="some text" width="600" />
</h:form>
</composite:implementation>
然后在我的文件AttachmentsView.xhtml中,我使用以下组件:
<owncomponents:fileadder type="MEMO" />
但这并不是将支持bean中的typeString值设置为“MEMO”。它保持为空,我用一个打印值的按钮对它进行了测试
我如何使支持bean获得我设置为复合组件的
type
-属性为“MEMO”的typeString
的值?为什么它是null而不是MEMO?我通过以下方式手动从支持bean中的组件获取“type”属性来解决这个问题:
String typeString = (String) component.getAttributes().get("type");
您必须将目标bean/模型作为另一个复合属性传递。然后您可以在组合中使用它来设置属性
<cc:interface>
<cc:attribute name="bean" type="com.example.Bean" />
<cc:attribute name="type" type="java.lang.String" />
</cc:interface>
<cc:implementation>
<c:set target="#{cc.attrs.bean}" property="type" value="#{cc.attrs.type}" />
<p:editor value="#{cc.attrs.bean.text}" />
</cc:implementation>
请注意,我在组合外部考虑了表单。在复合材料中使用表单是一种糟糕的做法
另见:
<cc:interface>
<cc:attribute name="bean" type="com.example.Bean" />
<cc:attribute name="type" type="java.lang.String" />
</cc:interface>
<cc:implementation>
<c:set target="#{cc.attrs.bean}" property="type" value="#{cc.attrs.type}" />
<p:editor value="#{cc.attrs.bean.text}" />
</cc:implementation>
public class Bean {
private String text;
private String type; // I suggest to make it an enum.
// ...
}
<h:form>
<your:composite bean="#{bean}" type="MEMO" />
<p:commandButton action="#{bean.submit}" />
</h:form>