I have requirement to display component dynamically, I get the component name in list. Using list name I have to render the component, for now I have taken one component (i.e. component1
).
I have render that component inside a Visualforce page. Code is as below. Please advise.
Visualforce Component
<apex:component>
<div>
<h1>Component 1 </h1>
</div>
</apex:component>
Visualforce page
<apex:page controller="cntr" >
<c:{!compName} />
</apex:page>
Controller
public class cntr {
public String compName { get; private set; }
public cntr() {
compName = "component1"
}
}
Updated
Considering your answer, i tried Same on component1.
- Keeping "component1" same.
Apex Page
<apex:page controller="comcntr">
<apex:dynamicComponent componentValue="{!createComponent}"/>
</apex:page>
controller
public class comcntr {
public comcntr() { }
public Component.c.component1 getCreateComponent() {
Component.c.component1 myDy = new Component.c.component1();
return myDY;
}
}
this working for me now .
But my requirement is like, inside the controller i have the property called compName it is holding my component name. i wanted render that component.
public class comcntr {
public String compName='component1';
public comcntr() { }
public Component.c.component1 getCreateComponent() {
Component.c.component1 myDy = new Component.c.component1();
return myDY;
}
}
By taking comName value as component name, i have to return the the component. Can you suggest how can achieve this? My compName is always changes depending on requirement.