Visualforce Custom Controller : In our previous Salesforce Tutorial we have clearly learned about Visualforce Standard Controller. In this Salesforce Training tutorial we are going to learn about Visualforce Custom controller actions that are invoked to visualforce page.
Visualforce Custom Controller
When a developer needs different logic and functionality he/she may write their own Apex controller class. Custom controller’s will not provide default functions like standard controllers. Custom apex code must be written for custom controllers.
Syntax :- <apex : page controller = “Mycontroller”></apex:page>
Visualforce Custom controller requires custom apex code to be bounded with visualforce page. When writing custom controller we have to define two actions.They are
Exposing Data.
In controller, page components can use expressions to bind data by reference. When data in visualforce page is modified controller can also be modified. if we want to provide read-only or read and write access to data we have to provide getter and setter methods or both to variables.
Writing Action methods.
Action methods are public, non static controllers methods that return a pagereference object or null. If null, visualforce current page is refreshed and if it is not null pagereference is used to determine the location of the new page. Action methods in custom controllers have some purpose. They are
- To preserve View State.
At runtime Force.com preserves View state of the page and send back to server for invocation of an action. By using view state we can build multiple page without writing code.
- To invoke custom logic.
To invoke custom logic like updating, inserting records to a data base we use DML methods. Along with constructors, action methods are used to write new business logic and also used to call existing apex code in visualforce controller.