How do I pass the state of variables from one page to another, if both pages use the same controller, but the second page uses a controller extension?

advertisements

I have a page that needs to pass data to another page. They both use the same controller, but the second page has its own controller extension. When the first page passes data to the second page, the second page will recall the constructor of the controller. If I remove the controller extension from the second page, data can be passed and the constructor is only called on the first page, not on the second.

How can I pass state from one page to another, if both pages use the same controller, but one page uses a controller extension?

For example, in the following code, if I enter data in the outputText for the variable t and then go to the next page, the state won't save, the constructor will be recalled, and the value for t on page 2 will be "construct". If the testExtension is removed from the second page, then everything works fine.

apex/test1 VisualForce

<apex:page controller="test">
    <apex:outputText value="{!t}" />
    <apex:commandButton action="/test2" value="Go to test2" />
</apex:page>

Controller

class test {
    public String t {get; set;}
    public test() {
        t = 'construct';
    }
}

apex/test2 VisaulForce

<apex:page controller="test" extension="testExtension">
    <apex:outputLabel value="{!t}" />
</apex:page>

controller extension:

class testExtension {
    public testExtension(test controller) {
    }
}


public with sharing class Fake01_Controller {

    public String fake02String {get;set;}

    public PageReference fake03Submit() {
        PageReference prToNavigate = null;

        prToNavigate = Page.Fake_02;

        return prToNavigate;
    }

}

Fake 01 page:

<apex:form >

    <apex:inputText value="{!fake02String}" />

    <apex:commandButton action="{!fake03Submit}"
        value="Navigate to Fake 02"
    />

</apex:form>

Fake 02 page:

<apex:form >

    <apex:outputText value="{!fake02String}" />

</apex:form>

Please, read PageReference about setRedirect method:

If set to false, the redirect is a server-side forward that preserves the view state if and only if the target page uses the same controller and contains the proper subset of extensions used by the source page.