org.apache.click.ajax
Interface AjaxBehavior

All Superinterfaces:
Behavior
All Known Implementing Classes:
DefaultAjaxBehavior

public interface AjaxBehavior
extends Behavior

AjaxBehavior extends the basic Behavior functionality to allow Controls to handle and process incoming Ajax requests.

To handle an Ajax request, AjaxBehavior exposes the listener method: onAction. The onAction method returns an ActionResult that is rendered back to the browser.

Before Click invokes the onAction method it checks whether the request is targeted at the AjaxBehavior by invoking the method Behavior.isAjaxTarget(). Click will only invoke onAction if isAjaxTarget returns true.


Method Summary
 boolean isAjaxTarget(Context context)
          Return true if the behavior is the request target, false otherwise.
 ActionResult onAction(Control source)
          This method can be implemented to handle and respond to an Ajax request.
 
Methods inherited from interface org.apache.click.Behavior
preDestroy, preRenderHeadElements, preResponse
 

Method Detail

onAction

ActionResult onAction(Control source)
This method can be implemented to handle and respond to an Ajax request. For example:
 public void onInit() {
     ActionLink link = new ActionLink("link");
     link.addBehaior(new DefaultAjaxBehavior() {

         public ActionResult onAction(Control source) {
             ActionResult result = new ActionResult("<h1>Hello world</h1>", ActionResult.HTML);
             return result;
         }
     });
 } 

Parameters:
source - the control the behavior is attached to
Returns:
the action result instance

isAjaxTarget

boolean isAjaxTarget(Context context)
Return true if the behavior is the request target, false otherwise.

This method is queried by Click to determine if the behavior's onAction(org.apache.click.Control) method should be called in response to a request.

By exposing this method through the Behavior interface it provides implementers with fine grained control over whether the Behavior's onAction(org.apache.click.Control) method should be invoked or not.

Below is an example implementation:

 public CustomBehavior implements Behavior {

     private String eventType;

     public CustomBehavior(String eventType) {
         // The event type of the behavior
         super(eventType);
     }

     public boolean isAjaxTarget(Context context) {
         // Retrieve the eventType parameter from the incoming request
         String eventType = context.getRequestParameter("type");

         // Check if this Behavior's eventType matches the request
         // "type" parameter
         return StringUtils.equalsIgnoreCase(this.eventType, eventType);
     }

     public ActionResult onAction(Control source) {
         // If isAjaxTarget returned true, the onAction method will be
         // invoked
         ...
     }
 } 

Parameters:
context - the request context
Returns:
true if the behavior is the request target, false otherwise