How can I create a JSF composite component that al

2019-08-14 05:31发布

问题:

I'm trying to create a composite component which allows a user to toggle between a selectOneMenu and selectManyListbox. I want the toggle to be bindable to a boolean value and the selectOneMenu/selectManyListbox bindable to a list of objects in the view-scoped backing bean of the page.

I'm able to create a composite component that can read the variables easily enough. I just get the attributes within the bound @FacesComponent object via getAttributes().

How do I go about making those variables writable though?

For example, say I have the following view scoped bean:

AssetSearch.java

@ManagedBean(name = "AssetSearch")
@ViewScoped
public class AssetSearch {

    private boolean toggle;
    private List<Asset> selectedList;

}

And I want to manipulate those variables with a composite component:

index.xhtml

<my:specialList toggle="#{AssetSearch.toggle}"
                selected="#{AssetSearch.selectedList}"/>

How can I manipulate these 2 variables within my composite component backing bean?:

specialList.xhtml

<cc:interface componentType="specialList">
    <cc:attribute name="toggle" type="java.langBoolean" required="true"/>
    <cc:attribute name="selected" type="java.util.List" required="true"/>
</cc:interface/>
<cc:implementation>
    <h:selectBooleanCheckbox value=#{#cc.attrs.toggle}/>
    <h:selectOneMenu rendered="#{cc.attrs.toggle}" 
                     value="#{cc.attrs.selected}">
       ...
    <h:selectManyListbox rendered=#{! cc.attrs.toggle}"
                         value="#{cc.attrs.selected}">
       ...
</cc:implementation>

SpecialList.java

@FacesComponent(value = "specialList")
public class SpecialList extends UIInput {

    ...

}

As I said, its pretty easy to get these variables with getAttributes() but I'm really not sure how to manipulate them. I did read through:

http://balusc.blogspot.com/2013/01/composite-component-with-multiple-input.html

I could probably use getSubmitedValue/getConvertedValue to manage the selectedList but I have a bunch of other variables I need to manipulate as well.

回答1:

As I said, its pretty easy to get these variables with getAttributes() but I'm really not sure how to manipulate them.

From the UIComponent#getAttributes() javadoc (emphasis mine):

Return a mutable Map representing the attributes (and properties, see below) associated wth this UIComponent, keyed by attribute name (which must be a String).

It's thus mutable. You can use the usual Map#put() method on it. Provided that you want to toggle a java.lang.Boolean attribute named "toggle", here's an example:

getAttributes().put("toggle", getAttributes().get("toggle") != Boolean.TRUE);