2

I have multiple VF pages that share one controller. My problem is that:

1.) If I set PageReference.setRedirect(false), it doesn't redirect the pages

2.) If I set PageReference.setRedirect(true), it invokes a new controller whenever we get to the redirected page so data does not persist

Should I move all my VF pages into one page and have the divs show/hide the different pages so I don't have to change the view state? I'm not sure how else I can both pass data and change the view state otherwise

4
  • in option 1 are you facing any issue? this always work for me.
    – Ratan Paul
    Dec 4, 2015 at 19:24
  • @Ratan I have my pages all in different VF files. The issue I'm facing is that it doesn't redirect to the next VF file (VF page). How would I make it do that?
    – user22261
    Dec 4, 2015 at 19:34
  • Have you tried using a static variable to hold things in between pages? Dec 4, 2015 at 19:40
  • @SebastianKessel how would this work?
    – user22261
    Dec 4, 2015 at 19:53

2 Answers 2

2

Just an example Hope this will help you

public class SearchController1 {

    public List<Account> accounts {get; set;}

    public SearchController1() {
        this.accounts = new List<Account>();
        checkCurrentPage();
    }
    public void checkCurrentPage() {
         system.debug('====check which page we are on===='+system.currentPageReference());
    }
    public void doSearch() {
        this.accounts = [select Id,Name from Account limit 200];
    }

    public PageReference doClick() {
        return Page.ViewPage2.setRedirect(false);
    }
}

ViewPage1 VF page

<apex:page controller="SearchController1" title="SearchPage" showHeader="true" sidebar="false" id="page">
    <apex:form id="form">
        <apex:pageBlock id="block">
            <apex:pageBlockButtons >
                <apex:commandButton value=" Search!! " title=" Search!! " action="{!doSearch}" reRender="form" />
                <apex:commandButton value=" Click!! " title=" Click!! " action="{!doClick}" rendered="{!accounts.size > 0}" />
            </apex:pageBlockButtons>
            <apex:pageBlockTable value="{!accounts}" var="item">
                <apex:column headerValue="{!$ObjectType.Account.Fields.Name.Label}">
                    <apex:outputText value="{!item.Name}" />
                </apex:column> 
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

ViewPage2 VF Page

<apex:page controller="SearchController1" title="ViewPage" showHeader="false" sidebar="false" id="page" action="{!checkCurrentPage}">
    <apex:form id="form">
        <apex:pageBlock id="block">
            <apex:pageBlockTable value="{!accounts}" var="item">
                <apex:column headerValue="{!$ObjectType.Account.Fields.Name.Label}">
                    <apex:outputText value="{!item.Name}" />
                </apex:column> 
            </apex:pageBlockTable>
        </apex:pageBlock>
     </apex:form>
</apex:page>

Chech checkCurrentPage method for which page we are on.

So when you set redirect = false. Onclick of Click!! button It will redirect to ViewPage2. In this page we are using same list account fetched in ViewPage1.

If you see browser top url is changed and still pointing to ViewPage1 but believe me It is redirect to viewPage2.

Try to create fresh page and controller.

Good Luck..

Note: Whenever we use setredirect = true false, URL doesn't change. so don't confused


Edit

If setRedirect = true

A redirect is performed through a client side redirect. This type of redirect performs an HTTP GET request, and flushes the view state, which uses POST

If setRedirect = 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.


Note that if the URL of the PageReference object is set to a website outside of the salesforce.comdomain, or to a page with a different controller or controller extension, the redirect always occurs, regardless of whether the redirect attribute is set to true or false.

3
  • Thanks! Is there a way we can differentiate between which page we are on, since the URL is all the same?
    – user22261
    Dec 4, 2015 at 22:30
  • Have a look at another answer. Hope this clarifies. Dec 5, 2015 at 1:02
  • @user22261 use system.currentPageReference() to check which page we are on. Updated Ans for same.
    – Ratan Paul
    Dec 5, 2015 at 4:22
3

The above answer gives the technical solution for the approach but doesn't clarify why this happens.

  1. SetRedirect (True)

    When you put SetRedirect as true for your pagerefrence, Browser recreates the whole page, be it a new page URL or the same page. You can verify all constructors or static blocks getting initialised in Debug logs. This means this is a server refresh.

  2. SetRedirect (False)- Default

    However in this scenario, the page is never submitted to the Server and all DOM elements are regenerated from client viewstat. This is a browser refresh of the page with available information.

To give an example

Create following page : Name it as "abc"

<apex:page controller="redirectVerfication">
    <apex:form>
        <apex:inputtext value="{!val}"/>
        <apex:commandbutton action="{!serverRefresh}" value="Server Refresh"/>
        <apex:commandbutton action="{!clientRefresh}" value="Client Refresh"/>
    </apex:form>
</apex:page>

Create following controller

Public Class redirectVerfication{

    Public String val{get;set;}

    Public Pagereference serverRefresh(){

        val='abc';
        PageReference pg = new PageReference('/apex/abc');
        pg.setRedirect(true);
        return pg;

    }

    public Pagereference clientRefresh(){

        val='abc';
        //Default setRedirect Value if false
        PageReference pg = new PageReference('/apex/abc');
        return pg;

    }
}

When you preview the page, you will see one input text and two buttons.

Set inputtext value as 'pqr' and press "Server refresh button". You will notice that the new value is blank. Because new DOM is generated. This button will never follow rerender property.

Set inputtext value as 'pqr' and press "Client refresh button". You will notice that the new value is 'abc'. As client refresh method updates the value and sends it back to browser and client generates the page.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .