Binding a managed bean instance to composite component

前端 未结 1 2006
挽巷
挽巷 2020-12-04 00:15

I have a composite component (collapsiblePanel). The component uses the \"collapsible\" bean to provide the toggle function. When I use the same component multiple times on

相关标签:
1条回答
  • 2020-12-04 01:00

    You can use the componentType attribute of the <cc:interface> to define a "backing component".

    E.g.

    <cc:interface componentType="collapsiblePanel">
        ...
    </cc:interface>
    <cc:implementation>
        ...
        <h:commandButton action="#{cc.toggle}" ... />
        ...
        <h:panelGroup rendered="#{!cc.collapsed}" ...>
        ...
    </cc:implementation>
    

    with just a com.example.components.CollapsiblePanel

    @FacesComponent(value="collapsiblePanel") // To be specified in componentType attribute.
    public class CollapsiblePanel extends UINamingContainer { // Important! Must extend UINamingContainer.
    
        private boolean collapsed;
    
        public void toggle() {
            collapsed = !collapsed;
        }
    
        public boolean isCollapsed() {
            collapsed;
        }
    
    }
    

    However, when you want to have multiple of those components, then you should declare physically separate instances of them in the view. If this needs to happen dynamically, then you need to use <c:forEach> to generate physically separate instances of them instead of <ui:repeat> with a single component. Otherwise you have to map all collapsed states by the client ID inside a Map<String, Boolean>. See for an example and more background information also Getting same instance of `componentType` in composite component on every use

    0 讨论(0)
提交回复
热议问题