1

I was working on a multi-step wizard that had a "master page" that contained the the common components (next, previous, etc). To make things easier, the children were using the same controller as the master page. However, this seems this causes to separate instances of the controller to be loaded, which in turn causes some unexpected results:

Duplicated Controllers in viewstate Only lets me expand 1 at a time, but both are identical

Surprisingly, everything actually works as expected most of the time. However, I found that if you redirect in the page "Action" things start to get weird.

Controllers out of sync Note that the second controller does not contain any set variables

The result seems to be inconsistent. In my real world wizard, it would carry the view state if the following action was initiated from the child page. I put together a simpler demo to try to figure out what was actually happening and it loses the viewstate no matter where the next page reference comes from. Interestingly if I return null, the viewstate remains.

Screen shot of Page 2 after redirect action from Page 1:

Notice that the outputted values are different

Screen shot of page 3 after clicking "Load 3" button:

View state disappears completely

Wondering if anyone can offer a better explanation for what is going on... Is it bad practice to share controllers across Composition parent/child? It appears so...

Also, the view state "viewer" disappears as soon as I click any actions so I can't look deeper into whats going on with the tool.

CODE

Controller:

public class TestController {
    public String test {get; set;}

    public PageReference redirectTo2(){
        test = 'Hello world';
        PageReference pf = Page.Page2;
        pf.setRedirect(false);
        return pf;
    }

    public PageReference load3(){
        PageReference pf = Page.Page3;
        pf.setRedirect(false);
        return pf;
    }
}

Parent/Template Page: Displays the test value and has a button to redirect to page 3.

<apex:page controller="TestController">
    <apex:form>
        <h1>Parent</h1>
        <div>
            value: {!test} 
            <br/>
            <apex:commandButton action="{!load3}" value="Load 3 from parent"/>
        </div>
        <apex:insert name="child" />
    </apex:form>
</apex:page>

Page 1: Just calls an action to set a value and redirect to page 2

<apex:page controller="TestController" action="{!redirectTo2}">
<apex:composition template="TestParent">
    <apex:define name="child">
    <h1>Child 1</h1>
    </apex:define>
    </apex:composition>
</apex:page>

Page 2: Has a button to redirect to Page 3

<apex:page controller="TestController">
    <apex:composition template="TestParent">
    <apex:define name="child">
    <h1>Child 2</h1>
        <div>
            value: {!test} <br/> 
            <apex:commandButton action="{!load3}" value="Load 3 from child"/>
        </div>
    </apex:define>
    </apex:composition>
</apex:page>

Page 3: just displays the results

<apex:page controller="TestController">
    <apex:composition template="TestParent">
        <apex:define name="child">
            <h1>Child 3</h1>
            <div>value: {!test}</div>
        </apex:define>
    </apex:composition>
</apex:page>
2

Action in the page may not respect view state. See here for an old thread for some insight.

https://developer.salesforce.com/forums/ForumsMain?id=906F000000096rNIAQ

  • i assume redirect is defaulted to false. I tested both ways. Same result – NSjonas Nov 22 '14 at 8:30
  • I updated the question to remove that confusing. You should delete or modify your answer – NSjonas Nov 22 '14 at 8:33
  • Ah you're using action. That's a no go according to here: developer.salesforce.com/forums/… – tsalb Nov 22 '14 at 8:37
  • interesting... seems very odd that it would maintain it for one of the controllers in viewstate but not the other. Still looking for additional info on using a single controller in composition – NSjonas Nov 22 '14 at 8:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.