4
votes

Edited question...

Hello,

I would like to load a .xhtml file of my composite component from a backing bean, and add it to the page dynamically. The name of the .xhtml file comes form a variable.

Ex.:

public MyBean (){

    String componentFile = "myCompositeComponent.xhtml"

    public String addComponentToPage(){

          //how do that?...

          return null;
    }

} 

Thank you!

2
To the point, the question makes no utter sense. Composite components exist of XHTML only, not of Java code. It's entirely unclear how the code of CCPageLoader look like, so it's impossible to guess/understand what you actually mean. This question will remain unanswered until you fix the terminology and/or supply some code so that the question is better understood. - BalusC
Hi BalusC, what I would like to do: load the xhtml file of the composite component and add it to the page. This needs to be done by the backing bean of the page. Thanks for help. - Fabio

2 Answers

6
votes

That's not possible. A composite component is template-based and can only be used in views. Your best bet is to repeat exactly the JSF code which you've originally written in the composite component in the model. Better would be to create a full worthy @FacesComponent class which extends UIComponent, complete with a @FacesRenderer. True, it's a tedious and opaque job, but this way you'll end up with a component which is reuseable in both the view and the model by a single code line.

An -ugly- alternative is to place all possible components in the view and use the rendered attribute.

<my:component1 rendered="#{bean.myComponentType == 'component1'}" />
<my:component2 rendered="#{bean.myComponentType == 'component2'}" />
<my:component3 rendered="#{bean.myComponentType == 'component3'}" />
...

Wrap this if necessary in a Facelets tag file so that it can be hidden away and reused in several places.

0
votes

I don't understand why do you want to add a composite component from a backing bean. I guess you want to make it visible dynamically in case of an event, but for that there is AJAX reRender.

For example you can do the following:

<h:panelGroup id="composite" rendered="#{myBean.renderComponent}">
    <my:compositecomponent/>
</h:panelGroup>

The renderComponent property stores a boolean value. You can switch that value and reRender composite with for e.g. Richfaces's <a4j:commandLink>.

Hope that helps, Daniel