So I made a composite component FileAdder.xhtml
<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>
And then I have the EditoriCompositeController ManagedBean:
@ViewScoped
@ManagedBean
public class EditoriCompositeController {
String typeString;
public void setTypeString(String typeStringParameter) {
this.typeString = typeStringParameter;
}
public String getTypeString() {
return typeString;
}
}
And then in my fileattachmentsview.xhtml I use the component:
<owncomponents:fileadder type="MEMO" />
But that is not setting the typeString value in the backing bean as "MEMO". It remains as null I tested it with a button that prints the value.
How can I make the backing bean get the value for typeString
I set to the composite component's type
-attribute as "MEMO"? Why it's null
and not "MEMO"?
You have to pass the target bean/model as another composite attribute. Then you can inside the composite use <c:set>
to set a property on it.
<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>
Usage:
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>
Note that I factored the form outside the composite. Having a form inside a composite is poor practice.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With