Controller Extensions

The final type of controller is the controller extension. A controller extension is a custom controller that extends the behavior of a standard controller. Controller extensions are primarily used to integrate Visualforce more tightly with the native user interface. Many features of Visualforce integration such as overriding standard buttons are not supported for pages that use custom controllers.

Custom controllers can be easily retrofitted to become controller extensions. Multiple extensions can be used in a single page, enabling a large monolithic controller to be divided into smaller controllers by behavior, where some pages might use only a subset of the behaviors.

Listing 6.8 illustrates a trivial controller extension class, and Listing 6.9 shows a page that uses it. The only difference between it and a custom controller is that a constructor is required, allowing the standard controller (StandardController for a single record or StandardSetController for multiple records) to be passed to the class. In a page that uses the controller extension, all the built-in actions from the standard controller are available implicitly, without any code.

Listing 6.8 Sample Controller Extension with Single Action Method


public class MyPageController6_8 {
  private ApexPages.StandardController controller;
  public MyPageController6_8(ApexPages.StandardController controller) {
    this.controller = controller;
  }
  public PageReference doSomething() { return null; }
}


Listing 6.9 Page Using Sample Controller Extension


<apex:page standardController="Project__c"
  extensions="MyPageController6_8">
  <apex:form>
    <apex:commandButton action="{!doSomething}"
      value="Do Something" />
  </apex:form>
</apex:page>


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.62.34