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
dfcdfc 

Controller Class Lifecycles

(1) Are VisualForce controller classes instantiated by the platform anew for multiple HTTP requests (of, say, the same page), or are they instantiated once for a user's entire session?

 

(2) If Page A uses controller class X, and Page B uses controller class X also, and a button on Page A performs logic and then navigates to Page B, is the same controller class instance of X used for both the "inbound" (from the user) Page A action and "outbound" (to the user) Page B rendering?

 

Thanks!


Dave

 

Best Answer chosen by Admin (Salesforce Developers) 
Anup JadhavAnup Jadhav

Hi dfc,

 

To answer your question:

 

 1) Yes

2)  What you've described here is a wizard behaviour. Please see my reply to Alan in this thread for more details.

 

- A J

All Answers

paul-lmipaul-lmi

1. For Ajax requests, the state is saved.  If you forward to another page, it will lose the controller state.

2. No, at least, I've never been able to carry state from one page to another using the same controller, without passing the info via URL params.

Rajesh ShahRajesh Shah
You can carry on a state from one Page to another page using the same controller by not setting the redirect attribute. This is how it is done in Wizards where there are multiple pages with the same controller.
paul-lmipaul-lmi
how do you force the controller to go to the next page without the setRedirect though?  If I put the pagereference in a page's button click, it won't go to the next page unless setRedirect is true.  is this stateful controller stuff dependent on not using Ajax?
Anup JadhavAnup Jadhav

Hi paul,

 

It can be done if you are creating a wizard, which is basically one controller mapped to multiple visualfoce pages.

 

You can find a good example in the Visualforce Developers guide on Page  79.

 

I've copied a code snippet from the example described in the book.

 

// when users move from page to page.
public PageReference step1() {
return Page.opptyStep1;
}
public PageReference step2() {
return Page.opptyStep2;
}
public PageReference step3() {
return Page.opptyStep3;
}

 

- A J

Anup JadhavAnup Jadhav

Hi dfc,

 

To answer your question:

 

 1) Yes

2)  What you've described here is a wizard behaviour. Please see my reply to Alan in this thread for more details.

 

- A J

This was selected as the best answer
paul-lmipaul-lmi
ah, figured it out.  you can't use the "status" attribute in the commandButton or Command link that is the uses the "action" attribute that returns the next page.  Using "status" makes the button/link ajax-enabled, which pretty much requires the setRedirect(true) method to be called on the PageReference.
dfcdfc
Thanks, everyone!