In salesforce we generally go with one visualforce page and one custom controller. Here i have come up with a solution where you can use same controller class for multiple pages. Below are the details of it:
1. Create a generic controller which will idetify which will identify the page which is currently being accessed.
public virtual class GenericControllerClass {
private String pageName;
public String getPageName(String url)
{
pageName='';
if(url.indexOf('/apex/') != -1)
{
if(url.indexOf('?') != -1 )
{
pageName=url.subString(url.indexOf('/apex/')+6,url.indexOf('?'));
}
else
{
pageName=url.subString(url.indexOf('/apex/')+6);
}
}
return pageName;
}
}
2. Now assume you have three visuaforcePages page1,page2,page3.
page1:
<apex:page controller="SingleControllerClass">
.......
<your code here>
.......
</apex:page>
page 2:
<apex:page controller="SingleControllerClass">
.......
<your code here>
.......
</apex:page>
page 3:
<apex:page controller="SingleControllerClass">
.......
<your code here>
.......
</apex:page>
3. Now create controller class "SingleControllerClass" for above pages:
public class SingleControllerClass extends GenericControllerClass
{
......
your page variables
......
public SingleControllerClass()
{
super();
String pageName=getPageName(ApexPages.currentPage()..getUrl());
if(pageName=="page1")
{
initPage1();
}
else ifpageName=="page2")
{
initPage2();
}
else if(pageName=="page3")
{
initPage3();
}
}
private void initPage1()
{
..................
initialize your page1 variables here.
.................
}
private void initPage2()
{
..................
initialize your page2 variables here.
.................
}
private void initPage3()
{
..................
initialize your page3 variables here.
.................
}
}
Above mentioned three steps is a very simple process to implement. Using above solution you would be able to reuse your code efficiently and it also eliminates initialization of other page variables.
1. Create a generic controller which will idetify which will identify the page which is currently being accessed.
public virtual class GenericControllerClass {
private String pageName;
public String getPageName(String url)
{
pageName='';
if(url.indexOf('/apex/') != -1)
{
if(url.indexOf('?') != -1 )
{
pageName=url.subString(url.indexOf('/apex/')+6,url.indexOf('?'));
}
else
{
pageName=url.subString(url.indexOf('/apex/')+6);
}
}
return pageName;
}
}
2. Now assume you have three visuaforcePages page1,page2,page3.
page1:
<apex:page controller="SingleControllerClass">
.......
<your code here>
.......
</apex:page>
page 2:
<apex:page controller="SingleControllerClass">
.......
<your code here>
.......
</apex:page>
page 3:
<apex:page controller="SingleControllerClass">
.......
<your code here>
.......
</apex:page>
3. Now create controller class "SingleControllerClass" for above pages:
public class SingleControllerClass extends GenericControllerClass
{
......
your page variables
......
public SingleControllerClass()
{
super();
String pageName=getPageName(ApexPages.currentPage()..getUrl());
if(pageName=="page1")
{
initPage1();
}
else ifpageName=="page2")
{
initPage2();
}
else if(pageName=="page3")
{
initPage3();
}
}
private void initPage1()
{
..................
initialize your page1 variables here.
.................
}
private void initPage2()
{
..................
initialize your page2 variables here.
.................
}
private void initPage3()
{
..................
initialize your page3 variables here.
.................
}
}
Above mentioned three steps is a very simple process to implement. Using above solution you would be able to reuse your code efficiently and it also eliminates initialization of other page variables.
No comments:
Post a Comment