The <apex:actionSupport>
component is used to add AJAX behavior to other Visualforce components. It allows you to invoke an Apex controller method from UI events like onchange
, onclick
, onblur
, etc., without refreshing the whole page.
Syntax
xmlCopyEdit<apex:inputText value="{!userName}">
<apex:actionSupport event="onchange" action="{!updateUser}" rerender="outputPanel"/>
</apex:inputText>
Key Attributes
Attribute | Description |
---|---|
event | Specifies the DOM event that triggers the action (e.g., onchange , onclick ). |
action | Apex method to be invoked. |
rerender | ID of the component to be refreshed after action execution. |
status | Optional. Associates with an <apex:actionStatus> to show loading indicator. |
immediate | Optional. If true , skips validations and updates. |
Example 1: Auto Update a Field on Change
xmlCopyEdit<apex:page controller="SupportExample">
<apex:form>
<apex:inputText value="{!userName}">
<apex:actionSupport event="onchange" action="{!updateGreeting}" rerender="greetingPanel"/>
</apex:inputText>
<apex:outputPanel id="greetingPanel">
<h3>{!greetingMessage}</h3>
</apex:outputPanel>
</apex:form>
</apex:page>
Apex Controller:
apexCopyEditpublic class SupportExample {
public String userName { get; set; }
public String greetingMessage { get; set; }
public void updateGreeting() {
greetingMessage = 'Hello, ' + userName + '!';
}
}
Example 2: Button Click with AJAX
xmlCopyEdit<apex:commandButton value="Submit">
<apex:actionSupport event="onclick" action="{!submitForm}" rerender="resultPanel"/>
</apex:commandButton>
When to Use <apex:actionSupport>
- When you want to avoid full-page reloads.
- To enhance user experience with partial page updates.
- For real-time input-based UI changes.
Best Practices
- Always specify the
rerender
attribute to update only necessary sections. - Combine with
<apex:actionStatus>
for better user feedback. - Use
immediate="true"
carefully—useful when skipping validations is needed.