I came across an easy way to Show Apex message at another page (code is given below). I have a situation where 'Save' and 'Cancel' buttons on page 1 are going to redirect to the Page 2 and display messages according to the button's action.' The best way to include a condition for displaying the message? Thanks.
Controller 1
public PageReference GoToPage2()
{
ApexPages.PageReference pr = new ApexPages.PageReference(Page.Page2);
pr.SetRedirect(true);
pr.GetParameters().Put('message', 'Hello, World!');
return pr;
}
Page 1
<apex:commandButton action="{!GoToPage2}" value="Page 2!"/>
Controller 2
public void Init()
{
String message = '' + ApexPages.CurrentPage().GetParameters().Get('message');
String pageHeaderReferer = ApexPages.currentPage().getHeaders().get('Referer');
// Use the referrer parameter to only show the message when coming from Page 1
if(pageHeaderReferer != null && pageHeaderReferer.containsIgnoreCase('Page1') && message != 'null')
{
ApexPages.CurrentPage().AddMessage(new ApexPages.Message(ApexPages.Severity.Info, message));
}
}
Page 2
<apex:page controller="Page2Controller" action="{!Init}">
<apex:pageMessages/>