A same issue occurred to me and I have found a workaround.
PROBLEM: I have a visualforce page that has a value coming from its constructor, this value is being assigned to a component used in the page and through the component, the value needs to be sent to the component controller.
VF Page:
<apex:page controller="TestCloneController"> <c:GenericPaginationComponent sObjectAPIName="{!sObjectAPiName}"/> </apex:page>
Page Controller:
public class TestCloneController { public String sObjectAPIName{get; set;} public TestCloneController() { sObjectAPIName= 'Account'; } }
The value sObjectAPIName is set to the component.
Component:
<apex:component controller="ComponentController"> <apex:attribute name="sObjectAPIName" type="String" required="true" description="API Name of the object" assignTo="{!objectName}"/> <p>{!sObjectAPIName}</p> </apex:component>
Here, you have to use the assignTo attribute to let the controller know in which variable the value of attribute has to be assigned.
Component Controller:
public class ComponentController { public Boolean getterSetterFlag = true; public String objectName; public void setobjectName (String s) { objectName = s; if(getterSetterFlag) { system.debug(objectName); //objectName will not be null here getterSetterFlag = false; } } public String getobjectName() { return objectName; } //constructor public ComponentController() { system.debug(objectName); //objectName will be null here } }
Code Explanation: The component constructor is called when the component is loaded because in the first line it says controller="ComponentController". Here the value of the attribute will be null.
The getter and setter for the variable calls a debug, here the values have been set.
NOTE: The getter and setter methods are called twice, and therefore flag variable has been taken to make it run only once. Not sure why this behaviour is possessed.
Please upvote this if it solves your problem, to help others looking for the similar issues.