+ Start a Discussion
Shree KShree K 

difference between ActionSupport and ActionFunction?

Hi
came across the below statements while reading about the difference between Actionsupport and Actionfunction

1. Action function can call the controller method directly from java script.
2. Action support adds AJAX support to another visualforce component and then call the controller method.
  It makes sense to me that,

--ActionFunction works only with the help of javascript(that invokes controller method),but
--actionupports does not need any javascript to call the controller method
--Actionfunc can be used for popup message etc,which cant be done by Actionsupport
--We can use one controller method for multiple Actionfunction components on a vf page
     Are my above conclusions right till here or please correct me if i am wrong.
 
I wonder what is meant by adding ajax request to another component in Action support statement. I mean
For exanple...we can use command button for both actionSup and ActionFuc and onclick of those buttons action will be performed as (render rerender renderas etc),so here buttons are components then where is another component here, and both are doing same things almost, besides actionFunc is also working on Onclik basis even though without having such event in the syntax.
 
lastly, if i have two command buttons with same name "Save" at different places on my vf page and required to define Actionsupport for them,do i have to create seperate methods for those two buttons or can i use one method for those two buttons
 
William TranWilliam Tran
They are similar in many ways.

The main difference is:

Actionfunction is a function that can be called by any component via onclick, ondbclick, onblur event of that component

ActionSupport is added to a particular component like button or outputtext and the event is controlled by the actionsupport not the component thus actionsupport help enhance the compononment itself by being part of the component itself.

"lastly, if i have two command buttons with same name "Save" at different places on my vf page and required to define Actionsupport for them,do i have to create seperate methods for those two buttons or can i use one method for those two buttons"

Each button would need it's own action support tag but both action supports can call the same method in the controller like action="{!Save}"

Here's a good article for the differences:

http://www.cloudforce4u.com/2013/06/difference-between-action-support-and.html

Thx

Good Luck
Amit Chaudhary 8Amit Chaudhary 8
1. Difference is in case of Action function we invoke AJAX using Java script while in case of Action support we may directly invoke method from controller

 2. Other difference is Action function may be commonly used from different place on page while action support may only be used for particular single apex component


ActionFunction : Provides support for invoking controller action methods directly from JavaScript code using an AJAXrequest
Used when we need to perform similar action on various events. Even though you can use it in place of actionSupport as well where only event is related to only one control.
Example:-
<!-- Page: -->
<apex:page controller="exampleCon">
<apex:form>
<!-- Define the JavaScript function sayHello-->
<apex:actionFunction name="sayHello" action="{!sayHello}" rerender="out"
status="myStatus"/>
</apex:form>
<apex:outputPanel id="out">
<apex:outputText value="Hello "/>
<apex:actionStatus startText="requesting..." id="myStatus">
<apex:facet name="stop">{!username}</apex:facet>
</apex:actionStatus>
</apex:outputPanel>
<!-- Call the sayHello JavaScript function using a script element-->
<script>window.setTimeout(sayHello,2000)</script>
<p><apex:outputText value="Clicked? {!state}" id="showstate" /></p>
<!-- Add the onclick event listener to a panel. When clicked, the panel triggers
the methodOneInJavascript actionFunction with a param -->
<apex:outputPanel onclick="methodOneInJavascript('Yes!')" styleClass="btn">
Click Me
</apex:outputPanel>
<apex:form>
<apex:actionFunction action="{!methodOne}" name="methodOneInJavascript"
rerender="showstate">
<apex:param name="firstParam" assignTo="{!state}" value="" />
</apex:actionFunction>
</apex:form>
</apex:page>
 
/*** Controller ***/
public class exampleCon {
String uname;
public String getUsername() {
return uname;
}
public PageReference sayHello() {
uname = UserInfo.getName();
return null;
}
public void setState(String n) {
state = n;
}
public String getState() {
return state;
}
public PageReference methodOne() {
return null;
}
private String state = 'no';
}

ActionSupport : A component that adds AJAX support to another component, allowing the component to be refreshed asynchronously by the server when a particular event occurs, such as a button click or mouseover.?
Used when we want to perform an action on a particular event of any control like onchange of any text box or picklist.
eg
 
<apex:page controller="exampleCon">
<apex:form>
<apex:outputpanel id="counter">
<apex:outputText value="Click Me!: {!count}"/>
<apex:actionSupport event="onclick"
action="{!incrementCounter}"
rerender="counter" status="counterStatus"/>
</apex:outputpanel>
<apex:actionStatus id="counterStatus"
startText=" (incrementing...)"
stopText=" (done)"/>
</apex:form>
</apex:page>
 
/*** Controller: ***/
public class exampleCon {
Integer count = 0;
public PageReference incrementCounter() {
count++;
return null;
}
public Integer getCount() {
return count;
}
}

Please check below below blog for more information
http://www.cloudforce4u.com/2013/06/difference-between-action-support-and.html

http://www.slideshare.net/AbdulMujebb/difference-betweenactionsupport

Please let us know if this will help you

Thanks,
Amit Chaudhary