Using Extension with Standard/Custom controllers
A controller extension is any Apex class containing a constructor that takes a single argument of type
ApexPages.StandardController
orCustomControllerName
, where CustomControllerName is the name of a custom controller you want to extend.
Use controller extensions when:
- Standard Controller :- You want to leverage the built-in functionality of a standard controller but override one or more actions, such as edit, view, save, or delete.
- Custom Controller :- You want to add new actions or you can use a method in a different controller (which helps to eliminate code duplication)
Sample Extension class for Standard Controller
public class stdControllerExtension {
public stdControllerExtension(ApexPages.StandardController sc) {
// your code
}
...
// your methods goes here
...
}
Sample Visualforce page
<apex:page standardController="Account" extensions="stdControllerExtension">
Sample Extension class for Custom Controller
Lets say your custom controller name is DemoController
then you can add extension as follows
public class controllerExtension {
public controllerExtension(DemoController dc) {
// your code
}
...
// your methods goes here
...
}
Sample Visualforce page
<apex:page controller="DemoController" extensions="controllerExtension">