function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
NinoJoseNinoJose 

Reason why action attribute of apex:page should not be used for initialization

Hi Guys,
 
I'm fairly new with VisualForce development although I had some experience using Apex Code,SControl and a little of Flex. I was doing some practice developing visualforce pages and reading the component reference page when I saw that the documentation says that on <apex:page>,  "the action attribute should not be used for initialization".  What could be the reason?
 
I tried initializing the variable when its declared like below:
 
public class sampleController{
   Integer x = getCounter();
  
    public Integer getCounter(){
     ...
     }
}
 
And I also used the attribute action for a page example:
 
VF Page
<apex:page action="{!init}" controller="sampleController">
...
</apex:page>
 
Controller
public class sampleController{
     Integer x = 0;
     public PageReference init(){
          x = getCounter();
          return null;
     }
 
     public Integer getCounter(){
     ...
     }
}
 
And I don't see any difference on using these 2 methods of initializing. Could it be that the two methods differ on the number of Roundtrips for Client to Server?
 
Any thoughts would be helpul.
 
Thanks,
 
Nino


Message Edited by NinoJose on 12-30-2008 01:18 PM
jwetzlerjwetzler
If you want to do initialization you should do it in the constructor of your controller, since that will happen when the instance of the class is created.  The action attribute is really only useful for redirecting, specifically conditional redirecting.

The reason that we don't suggest doing initialization there is because we don't promise that your controller methods will fire in any particular order, other than that your constructor will be called first (and other things like on form submit we will call your setters before your action method, etc.).  Were the underlying lifecycle to change even a little, for example causing some getters to fire before your action method gets called, it could cause problems if you're relying on some sort of initialization to happen in your action method.  So it works now, but that doesn't mean it always will.

Really it's just an inappropriate place to do initialization.  If you see an attribute named "action" it should imply you're wishing to perform some kind of redirect or update to your page.
dchasmandchasman
+1

... and using the constructor for what its meant for (to guarantee a newly created instance will be 100% viable regardless of how it is created) becomes very important in your automated test methods where your code is the thing doing the creating and will only invoke the action method if you specifically call it. There is no way to get things out of phase with the ctor based approach.
NinoJoseNinoJose

Hi Guys,

Thanks for the reply. It makes sense. It should clear up my confusion.

Nino

Mathieu Beausoleil 7Mathieu Beausoleil 7
So what do you suggest if we have any @future to execute from the constructor?

@future call currently not allowed 

And what would be a good reason why we can't redirect from the constructor? So we have to initialize all the login in the constructor to finally be redirected by the action attribute? :/