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
Abers55Abers55 

Custom Controller and Extension communication

I'd like to share some data across 2 visualforce pages,  so I developed 2 pages that share a single controller:

PageOne

<apex:page controller="PageOne">
    <apex:form >
        <apex:inputText value="{!ThingOne}"/>
        <apex:commandButton value="Next" action="{!Next}"/>
    </apex:form>
</apex:page>

 and PageTwo 

<apex:page controller="PageOne">
    <apex:form >
        <apex:inputText value="{!ThingOne}"/>
        <apex:commandButton value="Previous" action="{!Previous}"/>
    </apex:form>
</apex:page>

 share

public with sharing class PageOne {

    public PageReference Previous() {
        return new PageReference('/apex/PageOne');
    }

    public PageReference Next() {
        return new PageReference('/apex/PageTwo');
    }

    public String ThingOne { get; set; }
}

 Which works fine and I'm happy.

 

However,  supposing Page One had a great deal of complexity and Page Two had a very simple visual output that was based on a result of this complexity.  I'd like to remove the complexity from the work to be done by Page Two,  so,  it seemed to me that the best way to do this would be to continue to share the custom controller and to use an extension on each page.  The code now reads:

PageOne:

<apex:page controller="PageOne" extensions="PageOneExt">
    <apex:form >
        <apex:inputText value="{!ThingOne}"/>
        <apex:commandButton value="Next" action="{!Next}"/>
    </apex:form>
</apex:page>
public with sharing class PageOneExt {
    public PageOne pageController;

    public PageOneExt(PageOne controller) {
        pageController = controller;
        system.debug('***pageController: ' + pageController);
    }

}

 

PageTwo:

<apex:page controller="PageOne" extensions="PageTwoExt">
    <apex:form >
        <apex:inputText value="{!ThingOne}"/>
        <apex:commandButton value="Previous" action="{!Previous}"/>
    </apex:form>
</apex:page>

 

public with sharing class PageTwoExt {
    public PageOne pageController;
    
    public PageTwoExt(PageOne controller) {
        pageController = controller;
/*
* Do a work here with data from Page One
*/ system.debug('***pageController: ' + pageController); } }

 However,  the ThingOne string is no longer shared between the pages and I don't understand why.  Am I doing something wrong or missing the point somewhere?

 

Best Answer chosen by Abers55
nickwick76nickwick76

I believe this has to do with the view state. As long as the two pages share the same controller and extensions the view state is preserved. But if they differ you will get a new viewstate and this the value of ThingOne is not stored in memory anymore.

 

If you set them both to the same extension you will see that the view state is preserved as well.

 

I don't know what you want to accomplish, but an idea could be to have one page with a controller and perhaps an extension containing several components. You can pass the controller down to the components and choose to display any of them using logic in your controller. This way you will only have to load your logic once.

 

// Niklas