Hi Awesome Developers,
Scenario 1:
Main Page
<apex:page standardController="Account" extensions="PageRedirectDemo">
<apex:form>
<apex:inputtext value="{!strReceivedText}" /> <br/>
<apex:inputtext value="{!objAccount.Id}" />
<apex:commandbutton action="{!redirect}" value="Redirect"/>
</apex:form>
</apex:page>
Controller :
public with sharing class PageRedirectDemo
{
public string strReceivedText{get;set;}
public Account objAccount{get;set;}
public PageRedirectDemo(ApexPages.StandardController stdController){
objAccount = (Account)stdController.getRecord();
}
public pagereference redirect(){
PageReference ref = new PageReference('/apex/RedirectedPage'+'?id='+objAccount.Id);
ref.setRedirect(false);
return ref;
}
}
Redirected Page
<apex:page standardController="Account" extensions="PageRedirectDemo">
<apex:outputLabel>Value received from Page 1</apex:outputLabel> <br/>
<apex:outputText> {!strReceivedText } </apex:outputText>
</apex:page>
Output :
The above will work well if I keep the signature of second page same as first. Now I have changed the signature of the second page as below and added empty constructor in the controller.
<apex:page controller="PageRedirectDemo">
<apex:outputLabel>Value received from Page 1</apex:outputLabel><br/>
<apex:outputText> {!strReceivedText } </apex:outputText> <br/>
<apex:outputtext value="{!objAccount.Id}" />
</apex:page>
public with sharing class PageRedirectDemo
{
public string strReceivedText{get;set;}
public Account objAccount{get;set;}
public PageRedirectDemo(){
}
public PageRedirectDemo(ApexPages.StandardController stdController){
objAccount = (Account)stdController.getRecord();
}
public pagereference redirect(){
PageReference ref = new PageReference('/apex/RedirectedPage'+'?id='+objAccount.Id);
ref.setRedirect(false);
return ref;
}
}
You can see the values are not set due to change in signature,
Scenario 3 :
MainPage :
<apex:page controller="PageRedirectDemo">
<apex:form>
<apex:inputtext value="{!strReceivedText}" /> <br/>
<apex:commandbutton action="{!redirect}" value="Redirect"/>
</apex:form>
</apex:page>
public with sharing class PageRedirectDemo
{
public string strReceivedText{get;set;}
public PageRedirectDemo(){
}
public pagereference redirect(){
PageReference ref = new PageReference('/apex/RedirectedPage');
ref.setRedirect(false);
return ref;
}
}
RedirectedPage
<apex:page controller="PageRedirectDemo">
<apex:outputLabel>Value received from Page 1</apex:outputLabel><br/>
<apex:outputText> {!strReceivedText } </apex:outputText> <br/>
</apex:page>
Output :
*IMP : Observe the URL in each scenario :)
Happy Redirecting ;)
Have you ever thought of redirecting to multiple pages using a single controller, here is what I have come up with the scenarios of redirecting to different pages using same controller.
Scenario 1:
Main Page
<apex:page standardController="Account" extensions="PageRedirectDemo">
<apex:form>
<apex:inputtext value="{!strReceivedText}" /> <br/>
<apex:inputtext value="{!objAccount.Id}" />
<apex:commandbutton action="{!redirect}" value="Redirect"/>
</apex:form>
</apex:page>
Controller :
public with sharing class PageRedirectDemo
{
public string strReceivedText{get;set;}
public Account objAccount{get;set;}
public PageRedirectDemo(ApexPages.StandardController stdController){
objAccount = (Account)stdController.getRecord();
}
public pagereference redirect(){
PageReference ref = new PageReference('/apex/RedirectedPage'+'?id='+objAccount.Id);
ref.setRedirect(false);
return ref;
}
}
Redirected Page
<apex:page standardController="Account" extensions="PageRedirectDemo">
<apex:outputLabel>Value received from Page 1</apex:outputLabel> <br/>
<apex:outputText> {!strReceivedText } </apex:outputText>
</apex:page>
Output :
The above will work well if I keep the signature of second page same as first. Now I have changed the signature of the second page as below and added empty constructor in the controller.
<apex:page controller="PageRedirectDemo">
<apex:outputLabel>Value received from Page 1</apex:outputLabel><br/>
<apex:outputText> {!strReceivedText } </apex:outputText> <br/>
<apex:outputtext value="{!objAccount.Id}" />
</apex:page>
public with sharing class PageRedirectDemo
{
public string strReceivedText{get;set;}
public Account objAccount{get;set;}
public PageRedirectDemo(){
}
public PageRedirectDemo(ApexPages.StandardController stdController){
objAccount = (Account)stdController.getRecord();
}
public pagereference redirect(){
PageReference ref = new PageReference('/apex/RedirectedPage'+'?id='+objAccount.Id);
ref.setRedirect(false);
return ref;
}
}
You can see the values are not set due to change in signature,
Scenario 3 :
MainPage :
<apex:page controller="PageRedirectDemo">
<apex:form>
<apex:inputtext value="{!strReceivedText}" /> <br/>
<apex:commandbutton action="{!redirect}" value="Redirect"/>
</apex:form>
</apex:page>
public with sharing class PageRedirectDemo
{
public string strReceivedText{get;set;}
public PageRedirectDemo(){
}
public pagereference redirect(){
PageReference ref = new PageReference('/apex/RedirectedPage');
ref.setRedirect(false);
return ref;
}
}
RedirectedPage
<apex:page controller="PageRedirectDemo">
<apex:outputLabel>Value received from Page 1</apex:outputLabel><br/>
<apex:outputText> {!strReceivedText } </apex:outputText> <br/>
</apex:page>
Output :
*IMP : Observe the URL in each scenario :)
Happy Redirecting ;)
No comments:
Post a Comment