Action components allow the page to invoke a method on the controller. The controller method typically performs some operation on the contents of the page, such as updating the database, and then either refreshes the page or navigates to a new page.
Before any refreshing or navigation takes place, the state of the user interface input elements on the page is injected into the variables of the controller using setters. This way, they are accessible from within your action code.
The two basic action components are commandButton
and commandLink
. The commandButton
is rendered as an HTML button, whereas the commandLink
is rendered as an anchor. Both are valid only inside a form
component. They are typically used with an action
attribute that specifies the name of the controller method to invoke or the URL of a new page to navigate to and a value
attribute that displays a label to the user. Listing 6.14 is an example of using the commandButton
, which invokes the doSomething
method of the controller when clicked.
<apex:form>
<apex:commandButton action="{!doSomething}"
value="Do Something" />
</apex:form>
The page
component also has an action, specified in the init
attribute. This action is called automatically upon page load but should not be used for initialization code. Its purpose is to immediately redirect the user to a new page.
Before invoking a controller method, all action components perform validation on data components, accepting user input that is contained within their parent form
. For example, if an input component is required but no value is provided, an error results. Errors can be displayed using the pageMessages
or messages
component (described in the “Error Handling” subsection of this chapter) and beside any inputField
components if their database field is defined to do so. You can disable this validation behavior by setting the action component’s immediate
attribute to true
.
Note
Visualforce includes actions that operate asynchronously, allowing modifications to the page without a full page refresh. These actions are discussed in Chapter 7.
18.117.71.211