org.apache.click.ajax
Class DefaultAjaxBehavior

java.lang.Object
  extended by org.apache.click.ajax.DefaultAjaxBehavior
All Implemented Interfaces:
AjaxBehavior, Behavior

public class DefaultAjaxBehavior
extends Object
implements AjaxBehavior

Provides a default implementation of the AjaxBehavior interface.

This class also provides the method, addHeadElementsOnce, that subclasses can implement if they need to add HTML HEAD elements only to the first Control that this Behavior is registered with.

If this Behavior should add HTML HEAD elements to all the Controls it is registered with, rather implement preRenderHeadElements.


Field Summary
protected  boolean headElementsProcessed
          Indicates whether the Behavior HEAD elements have been processed or not.
 
Constructor Summary
DefaultAjaxBehavior()
           
 
Method Summary
protected  void addHeadElementsOnce(Control source)
          Provides a method for adding HTML HEAD elements to the first Control this Behavior was registered with.
 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.
 void preDestroy(Control source)
          This event occurs before the Control onDestroy event handler.
 void preRenderHeadElements(Control source)
          This event occurs after Behavior.preResponse(org.apache.click.Control), but before the Control's Control.getHeadElements() is called.
 void preResponse(Control source)
          This event occurs before the markup is written to the HttpServletResponse.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

headElementsProcessed

protected boolean headElementsProcessed
Indicates whether the Behavior HEAD elements have been processed or not.

Constructor Detail

DefaultAjaxBehavior

public DefaultAjaxBehavior()
Method Detail

onAction

public ActionResult onAction(Control source)
Description copied from interface: AjaxBehavior
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;
         }
     });
 } 

Specified by:
onAction in interface AjaxBehavior
Parameters:
source - the control the behavior is registered with
Returns:
the action result
See Also:
AjaxBehavior.onAction(org.apache.click.Control)

isAjaxTarget

public boolean isAjaxTarget(Context context)
Description copied from interface: AjaxBehavior
Return true if the behavior is the request target, false otherwise.

This method is queried by Click to determine if the behavior's AjaxBehavior.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 AjaxBehavior.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
         ...
     }
 } 

Specified by:
isAjaxTarget in interface AjaxBehavior
Parameters:
context - the request context
Returns:
true if the behavior is the request target, false otherwise
See Also:
AjaxBehavior.isAjaxTarget(org.apache.click.Context)

preResponse

public void preResponse(Control source)
Description copied from interface: Behavior
This event occurs before the markup is written to the HttpServletResponse.

Specified by:
preResponse in interface Behavior
Parameters:
source - the control the behavior is registered with
See Also:
Behavior.preResponse(org.apache.click.Control)

preRenderHeadElements

public void preRenderHeadElements(Control source)
Description copied from interface: Behavior
This event occurs after Behavior.preResponse(org.apache.click.Control), but before the Control's Control.getHeadElements() is called.

Specified by:
preRenderHeadElements in interface Behavior
Parameters:
source - the control the behavior is registered with
See Also:
Behavior.preRenderHeadElements(org.apache.click.Control)

preDestroy

public void preDestroy(Control source)
Description copied from interface: Behavior
This event occurs before the Control onDestroy event handler. This event allows the behavior to cleanup or store Control state in the Session.

Specified by:
preDestroy in interface Behavior
Parameters:
source - the control the behavior is registered with
See Also:
Behavior.preDestroy(org.apache.click.Control)

addHeadElementsOnce

protected void addHeadElementsOnce(Control source)
Provides a method for adding HTML HEAD elements to the first Control this Behavior was registered with. This method will only be called once, passing in the first Control the Behavior was registered with.

Subclasses can implement this method instead of preRenderHeadElements(org.apache.click.Control) if HTML HEAD elements should only be added to one Control, even if the Behavior is added to multiple Controls.

Parameters:
source - the control the behavior is registered with