[SalesForce] Identify the current page name

In my apex controller how can I find out what my current page name is? I have need where I am redirecting from one page to another.
My first page does not have any parameters but my 2nd page has a parameter. I am using the same controller for both the pages. So when
I try to get the parameter name(part of page2) in constructor when page1 is loaded I get a null pointer exception because there is no parameter
for page1

I tried this but does not seem to work. Here page2 has a parameter called param1 and Page1 has no parameters. When the page1 is loaded first it goes to else condition and
throws a null pointer exception when I try to get that parameter:

if(ApexPages.currentPage() == Page.page1){
// do nothing

}else{
objectName = ApexPages.currentPage().getParameters().get('param1');
Amethod(objectName);
}

Any Suggestions?

Best Answer

You can parse the URL to get the name of the current page.

String pageName = ApexPages.currentPage().getUrl()
    .substringAfter('/apex/').substringBefore('?');
Related Topic