How to pass parameters using PageReference in Salesforce?

How to pass parameters using PageReference in Salesforce?

First VF Page:


<apex:page controller=”Sample”>
<apex:form >
    <apex:pageBlock >
        <apex:pageBlockSection >
            <apex:pageBlockSectionItem >Enter the text to be passed: <apex:inputText value=”{!strText}”/></apex:pageBlockSectionItem>
        </apex:pageBlockSection>
        <apex:pageBlockButtons >
            <apex:commandButton value=”Go to Next Page” action=”{!nextPage}”/>
        </apex:pageBlockButtons>
    </apex:pageBlock>
</apex:form>
</apex:page>


public class Sample {
    public String strText {get;set;}
    public Sample() {
    }
 
    public PageReference nextPage() {
        PageReference pg = new PageReference(‘/apex/NextPage’);
        pg.getParameters().put(‘str’, strText);
        pg.setRedirect(false);
        return pg;
    }
}


Second VF Page:


<apex:page controller=”NextPageController”>
Value passed in previous page is {!strText}
</apex:page>


public class NextPageController {
    public String strText {get;set;}
    public NextPageController() {
        strText = ApexPages.currentPage().getParameters().get(‘str’);
    }
}

Output:






Cheers!!!

Leave a Reply