Source Viewer : WEB-INF/classes/org/apache/click/examples/page/ajax/AjaxFallbackPage.java
package org.apache.click.examples.page.ajax;
import org.apache.click.ActionListener;
import org.apache.click.Control;
import org.apache.click.ActionResult;
import org.apache.click.ajax.DefaultAjaxBehavior;
import org.apache.click.control.ActionLink;
import org.apache.click.examples.page.BorderPage;
/**
* Demonstrates how to handle an AJAX request and fallback to a non-AJAX request
* in case JavaScript is disabled.
*/
public class AjaxFallbackPage extends BorderPage {
private static final long serialVersionUID = 1L;
private ActionLink link = new ActionLink("link", "here");
public AjaxFallbackPage() {
link.setId("link-id");
addControl(link);
// If JavaScript is enabled, the AjaxBehavior will be called
link.addBehavior(new DefaultAjaxBehavior() {
@Override
public ActionResult onAction(Control source) {
// Formatted date instance that will be added to the
String now = format.currentDate("MMM, yyyy dd HH:MM:ss");
String msg = "AjaxBehavior <tt>onAction()</tt> method invoked at: " + now;
// Return an action result containing the message
return new ActionResult(msg, ActionResult.HTML);
}
});
// If JavaScript is disabled, the ActionListener will be called instead
link.setActionListener(new ActionListener() {
public boolean onAction(Control source) {
// Formatted date instance that will be added to the
String now = format.currentDate("MMM, yyyy dd HH:MM:ss");
String msg = "ActionListener <tt>onAction()</tt> method invoked at: " + now;
// Return an action result containing the message
addModel("msg", msg);
return true;
}
});
}
}