+ Start a Discussion
Ken_KoellnerKen_Koellner 

Can one than one page use the same instance of controller?

I think this is do-able.  I just want to make sure.

 

I remember an example from the VF class of a wizard for job applications that let you do Next/Prev over three pages.  I think the way it worked is that it used the same instance of the controller for all pages.

 

I want to double-check that that is do-able.  I have a constructor that gets a great deal of data and holds it in memory.  I want to go to/from a detail page without reinstantiating the controller.  If it reinstantiates the controller, it would be too inefficient.  If it can't be done, then I'd do it by using the same page and having sections rendered based on where the user is.  It would be more elegrant to use separate pages though.

 

-Ken

 

Cory CowgillCory Cowgill

This is very interesting.

 

I have seen controller resed on different pages, but I am not sure about them using the same instance of the controller.

 

I don't think this can be done. You specify the controller/extensions in the page attributes, but there is nothing there to specify to retain a previous controller instantiation. Each page request reinstantiates the controller I believe, unless you are doing actionsupport tags to do AJAX method invocations, in which case your not moving to a new page.

 

If you find an answer please update this post!

Ken_KoellnerKen_Koellner

The example from the class uses an extention on a standard controller (even though it is a custom object).

 

I'm not sure if that is needed to keep the same instance or not.

 

I suspect it is the same instance of the controller but not 100% sure.

 

Also, don't know if it has to be an extention to a standard controller if indeed it is dobable.

 

JimRaeJimRae

As long as each page references the same controller, and you make sure to use the setredirect(false) option, the viewstate should be maintained between pages.  One item I recall as a potential "Gotcha" is that the data items all need to be defined on the first page, regardless of which page they are used on. They can be hidden on the first page if they aren't needed there, and then exposed later by using the Render option.  There may be additional caveats, but I am not sure.

Take a look at the Opportunity wizard in the developers guide as an example of what you could do.

http://www.salesforce.com/us/developer/docs/pages/salesforce_pages_developers_guide.pdf

Ken_KoellnerKen_Koellner

Here's something that appears to work.  I load page one.  It doesn't contain one of the fields on page two.  Still it appears to be okay.

 

 

public with sharing class KenTestControllerMultiPage {

	public String data1 {get; set;}
	public String data2 {get; set;}
	public String data3 {get; set;}
	
	public KenTestControllerMultiPage () {
		data1 = 'one data';
		data2 = 'two data';
		data3 = 'three data';
		System.debug('xyzzy KenTestControllerMultiPage constructor');
	}
	
    public PageReference step1() {
		System.debug('xyzzy step1');
        return Page.KenTestMultiOne;
    }
    public PageReference step2() {
		System.debug('xyzzy step2');
    	return Page.KenTestMultiTwo;
    }
}

 

 

 

<apex:page controller="KenTestControllerMultiPage" >
 
  <apex:form id="theForm" > 
      <apex:pageBLock > 

         <apex:pageBlockButtons >
              <apex:commandButton action="{!step2}" value="Goto Two"/>
          </apex:pageBlockButtons>
 
          <apex:pageBlockSection title="My Section" columns="1" id="mySection" collapsible="false">
              <apex:inputText value="{!data1}"/>
            <!--   <apex:inputText value="{!data2}"/>  -->
              <apex:inputText value="{!data3}" />
          </apex:pageBlockSection>    
      </apex:pageBLock>
  </apex:form> 


</apex:page>

 

<apex:page controller="KenTestControllerMultiPage">

  <apex:form id="theForm" > 
      <apex:pageBLock >

         <apex:pageBlockButtons >
              <apex:commandButton action="{!step1}" value="Goto One"/>
          </apex:pageBlockButtons>

          <apex:pageBlockSection title="My Section" columns="1" id="mySection" collapsible="false">
              <apex:inputText value="{!data1}"/>
              <apex:inputText value="{!data2}"/>
              <apex:inputText value="{!data3}" />
          </apex:pageBlockSection>    
           
      </apex:pageBLock>
  </apex:form> 

</apex:page>

 

 

 

 

Rajesh U.Rajesh U.

Hi All,

 

first of all, sorry for tagging along the same thread. just wanted to make sure that the message goes to right people asap!!

 

I am building wizard (similar to 3 page wizard in the VF guide, using same controller),

On page 1, it displays list of items to select from,

on page 2, it displays hierarchical children of the item selected in page 1 and allows user to mark one or more hierarchy tree nodes (using checkbox), as well as allows user to select a node. Note that the first action marking the checkbox, also generates second select action. I have two actionFunction listening for these actions on page2 that refreshes part of the pages (the checkbox action does not need to refresh anything in reality).

on page 3, it displays items checked in page2

 

To maintain view state, I have list of values maintained. for page1, I have id of the object, for page 2, another id of the selected object, and a hashmap of objects that are checked (there is getter method to get values of the hashmap to display selected object contents on page3).

 

I am calling all the getter methods for all the viewstate on page1 to make sure they get into the view state. It seem to be working fine for values other than the hashmap that keeps state of checked items. Looking at the debug log (I have added many debug statement, especailly to display state of hashmap when getter to get values called), the hashmap state is not preserved correctly when it moves from page2 to page3 (i.e. even if items checked, the map is empty). Further dive into the debug log shows,

 

1. when an item checkbox is clicked on page2, it calls actionfunction tied to the check event, that calls the controller action to save check state and add object in hashmap, if checked. At this point, debug log shows correct values in the hashmap, so assume that it gets returned in viewstate. it rerenders the same thing that's supposed to be rendered in page3, however with rendered=false to hide from page2.

 

2. because of #1, it also calls actionfunction tied to select event, that calls controller action to process select event and set other data to render the appropriate section. The data gets rendered correctly. however the debug log generated at this point shows that the hashmap that was populated with #1 event got reinitilized and now empty (instead of showing what's checked in #1).

 

Now, if I modify my page2 to not listen to select event and hence no call to actionfunction for #2, the hashmap stays intact in view state and moving to page3 displays the correct object checked in page2.

 

I hope what I explained makes sense,

 

now questions are,

 

1. any idea what's going on from page2 to page3 losing hashmap content in viewstate?

 

2. Is it because of multiple actionfunction trying to refresh different part of the page?

 

3. Any hints on how to debug the issue further?

 

thanks,

-rajesh