AjaxContext

AjaxContext is the server-side context of a single Ajax request. It exposes the HTTP request and response and provides the channel through which AjaxAction sends changes back to the browser.

Overview

AjaxController creates the context and passes it to the action. Parameters come from getRequest(); sendClientAction() transmits the UI update or dialog.

This example uses AjaxContext to send an updated component back to the existing page.

CODE
Button button = new Button("Send Ajax request");
button.onClick(new AjaxActionUpdate());
layout.add(result).setId(AjaxActionUpdate.RESULT_ID);
RESULT
Click to update

Dialog example

This example uses AjaxContext to send a modal dialog, demonstrating that the response is not limited to replacing content.

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

A context exists for a request, not for the lifetime of a page. Do not store it for later use. Validate inputs before turning them into UI output.

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.