Skip to content
This repository has been archived by the owner on Sep 28, 2020. It is now read-only.

Actions and Controllers

Artem Shurygin edited this page May 10, 2016 · 3 revisions

What is an Action

All of the requests received by a Fuga application are handled by an Action.

Action is basically a Java method that processes the request and produces some result.

public Response index() {
  return ok("I'm a action");
}

Action must return a Response value, representing a HTTP response for the web client. In this example method ok() create a 200 OK response containing text/html body.

By default, response type is text/html. There are several useful methods to set another type: asText(), asJson(), as("whatever/youwant").

return ok("I'm a action").asText();

Controller

A Controller is a class that just extends a Controller class and groups several action methods.

public class ExampleController extends Controller {

    public Response index() {
        return ok("It works!");
    }

}

A Controller class contains many useful methods, such as ok(), notFound(), badRequest(), redirect(), proceed() Each of them generates a corresponding HTTP Response. Also it's contains three variables: ctx, urls and forms.

A ctx is just current context. By this variable you can get instances of current application and request. It contains headers, cookies, sessions, etc.

Request request = ctx.getRequest();

FugaApp app = ctx.getApp();

Session session = ctx.getSession();

To manipulating client URL's there is a Urls class instance that you can get from urls variable. That class contains that(), asset() and urlencode() methods.

String url = urls.that("page", 100);      // Returns "http://example.com/page/100"
String asset = urls.asset("common.css")   // Returns "http://example.com/assets/common.css"
String urlencoded = urls.urlencode("@#$") // Returns "%40%23%24"
Clone this wiki locally