Thursday 30 August 2012

Using a Custom Apex Controller with a Visualforce Page

You now have a Visualforce page that displays all of your merchandise records. Instead of using the default controller, as you did in the previous tutorial, you’re now going to write the controller code yourself. Controllers typically retrieve the data to be displayed in a Visualforce page, and contain code that will be executed in response to page actions, such as a command button being clicked.
In this lesson, you’ll convert the page from using a standard controller to using your own custom Apex controller. Writing a controller using Apexallows you to go beyond the basic behaviors provided by the standard controller. In the next lesson you’ll expand this controller and add some e-commerce features to change the listing into an online store.
To create the new controller class:
  1. Click Your Name | Setup | Develop | Apex Classes.
  2. Click New.
  3. Add the following code as the definition of the class and then click Quick Save.
    public class StoreFrontController {
    
        List<Merchandise__c> products;
        
        public List<Merchandise__c> getProducts() {
            if(products == null) {
                products = [SELECT Id, Name, Description__c, Price__c FROM Merchandise__c];
            }
            return products;
        }
    }
  4. Navigate back to your product catalog page at https://<your-instance>.salesforce.com/apex/Catalog, and open the Page Editor, if it’s not already open.
  5. Change the opening <apex:page> tag to link your page to your new controller class.
    <apex:page controller="StoreFrontController">
    Notice that the attribute name has changed from standardController to controller. You also remove the recordSetVarattribute, because it’s only used with standard controllers.
  6. Click Save to save your changes and reload the page. The only change you should see is that the Merchandise tab is no longer selected.
  7. Make the following addition to set the application tab style back to Merchandise.
    <apex:page controller="StoreFrontController" tabStyle="Merchandise__c">
  8. Notice that above the Page Editor tool bar there is now a StoreFrontController button. Click it to view and edit your page’s controller code. Click Catalog to return to the Visualforce page code.
    Visualforce Page Editor toolbars
    You’ll use this in the next lessons.

Tell Me More...

  • As in the previous lesson, the value attribute of the pageBlockTable is set to {!products}, indicating that the table component should iterate over a list called products. Because you are using a custom controller, when Visualforce evaluates the{!products}expression, it automatically looks for a method getProducts() in your Apex controller.
  • The StoreFrontController class does the bare minimum to provide the data required by the Visualforce catalog page. It contains that single method, getProducts(), which queries the database and returns a list of Merchandise__c records.
  • The combination of a public instance variable (here, products) with a getter method (getProducts()) to initialize and provide access to it is a common pattern in Visualforce controllers written in Apex.

No comments:

Post a Comment