AjaxAction

Ajax actions update the current page without navigating away. An AjaxAction is invoked by a client-side component, processes an AjaxContext on the server, and sends client actions back to the browser.

Overview

An AjaxAction does not return a Component. The browser receives PushActions such as UpdateElement or OpenModalDialog. This makes it suitable for live results, form feedback, and dialog interactions.

A button invokes the server, and the result is replaced in place. The surrounding documentation page stays loaded.

CODE
Button button = new Button("Send Ajax request");
button.onClick(new AjaxActionUpdate());

// UpdateElement replaces the component with this ID.
layout.add(result).setId(AjaxActionUpdate.RESULT_ID);
RESULT
Click to update

Dialog example

Open a dialog whose content is created by the Ajax action. This illustrates a second kind of PushAction rather than an element replacement.

CODE
Button button = new Button("Open server dialog");
button.onClick(new AjaxActionOpenDialog()
        .addParameter("message","Hello from the server"));
RESULT

How it works

CODE
public class AjaxActionUpdate extends AjaxAction{
    @Override
    public void init(){
        setActionMapper(AjaxControl.instance.getActionMapper());
    }

    @Override
    public void perform(AjaxContext context) throws Exception{
        context.sendClientAction(
            new PushAction.UpdateElement(RESULT_ID,new Text("Updated"))
        );
    }
}

Reading parameters

Parameters supplied on an action are available on the request.

CODE
String message = context.getRequest().getParameter("message");
context.sendClientAction(new PushAction.OpenModalDialog(dialog));

Design guidance

Choose AjaxAction when the user should remain on the current page. Keep request processing in the action and renderable UI in Components. Read incoming parameters through AjaxContext.

Full API reference

These examples introduce the most important behavior without attempting to document every method. For the complete reference, see https://doc.jdance.org.