Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set JSF composite component attribute to ManagedBean property?

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"?

like image 438
Steve Waters Avatar asked Feb 07 '23 23:02

Steve Waters


1 Answers

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.

See also:

  • When to use <ui:include>, tag files, composite components and/or custom components?
like image 100
BalusC Avatar answered Apr 29 '23 02:04

BalusC