Type Hierarchy:

The base class for web controllers. A controller must extend this class, its attributes are mapped to the request parameters.

Attributes of this class should:

  • declares the String type (later Boolean, Integer, etc…)
  • be shared
  • be immutable

A controller serves two purposes: . to serve a request . to produce an URL that will provide an address for this controller

shared class Index() extends Controller() {
  shared actual default Response handle() => Response.ok().body("Hello World");

Our Index controller:

  • extends the Controller interface
  • is mapped to the server root via the [route] annotation
  • overrides the handle method for serving the default page

Mapping a request parameter to the controller can be achieved by declaring a corresponding attribute:

shared class Greeter(String name) {
  shared actual default Response handle() => Response.ok().body("Hello ``name``");

The Greeter controller is mapped to the /greeter path and can be invoked with an URL like /greeter?name=Cayla.

The controller string produces an URL for invoking this controller, the URL is generated using:

  • the path declared by the route annotation
  • the controller attributes

Let's redefine our Index controller:

shared class Index() extends Controller() {
  shared actual default Response handle() => Response.ok().body("Say hello to <a href="``Greeter("Cayla")``">Cayla</a>");

The response body will contain an URL that will look like http://localhost:8080/greeter?name=Cayla. The string method redefined by the base Controller class:

  • takes care of generating the correct URL, including the parameter encoding
  • provides a safe way for generating URL that is enforced by the Ceylon compiler and is refactorable in the Ceylon IDE

When more context is required during the request, the invoke method can be overriden instead of the handle method. The RequestContext class provides access to the full content of the request.

shared actual String string

Generates an URL to address this controller

Refined declaration: string
Inherited Attributes
Attributes inherited from: Object
shared default Response handle()

Handle the request and return a response

shared default Response invoke(RequestContext context)

Invoke the controller with the specified request context

Inherited Methods
Methods inherited from: Object