3

Have some strange behavior of the URL that gets displayed in my browser after redirecting between pages with a shared controller.

Reproducible Code

public with sharing class FooController {

        //saves the quotes
    public PageReference toFoo1(){
        PageReference pr = Page.foo1;
        pr.setRedirect(false); 
        return pr;
    }

    public PageReference toFoo2(){
        PageReference pr = Page.foo2;
        pr.setRedirect(false); 
        return pr;
    }
}

<apex:page showHeader="true" sidebar="true" controller="FooController">
    <apex:form>
    <apex:commandButton styleClass="button quote-btn" value="to foo2" action="{!toFoo2}" />
    </apex:form>
</apex:page>

<apex:page showHeader="true" sidebar="true" controller="FooController">
    <apex:form>
    <apex:commandButton styleClass="button quote-btn" value="to foo1" action="{!toFoo1}" />
</apex:form>
</apex:page>

After the first redirect, query params are dropped but the path does not change. However, if I redirect again, then the URL is then updated to the previous page.

Example:

  • Starting on /apex/Foo1?bar=abc...
  • Click "To Foo2" button. URL now reads /apex/Foo1
  • Click "To Foo1" button. URL now reads /apex/Foo2
  • Click "To Foo2" button. URL now reads /apex/Foo1

Update

Setting redirect to true is not an option as I do not want my viewstate to change.

  • I've seen this previously and found it very annoying, if you need the viewstate i think you're stuck. Personally I left it as is since I didn't believe end users would look very closely at the URLs. If you don't need a view state you can use a tags instead of commandbuttons. – Ralph Callaway May 24 '16 at 17:52
  • you might just for fun try commandlink or outputlink to see if the behavior changes, i doubt it, but worth trying – Ralph Callaway May 24 '16 at 17:52
  • true, refreshing the page causes odd behavior but I'm not sure how often uses will actually manually do so. Also can lead to confusion if the client includes the url in a bug ticket. – NSjonas May 24 '16 at 17:57
  • FYI: commandlink has the same behavor and an outputlink doesn't preserve viewstate. – NSjonas May 24 '16 at 18:05
2
 pr.setRedirect(false);

Instead of this. do this

pr.setRedirect(true);

Because setRedirect(false) don't refresh the view state and keep the value of controller variable. So update this.

  • I don't want the viewstate to change – NSjonas May 24 '16 at 16:13
  • @NSjonas I think you are stuck here because this is SF native behavior. Instead of view state you can try cookies if it is small data/ – Tushar Sharma May 24 '16 at 18:17

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.