Subscribe / Unsubscribe Enewsletters | Login | Register

Pencil Banner

Jump into Java microframeworks, Part 1

Matthew Tyson | Oct. 28, 2015
Go extra lightweight, with Java microframeworks Spark, Ninja, and Play

Ninja views with FreeMarker

Open up index.ftl.html, which is the page that renders our localhost:8080 request. Consider the first couple of lines in Listing 2:

Listing 2. index.ftl.html

<#import "../layout/defaultLayout.ftl.html" as layout> 

<@layout.myLayout "Home page">

FreeMarker is importing a template, and then invoking it. In the template's header.ftl.html you'll see a simple HTML template. Now look at this line:

<a class="navbar-brand" href="/">${i18n("header.title")}</a>

This is a template token, which will eventually be replaced by a model value. You'll remember that when we changed the value in, the value was reflected in the generated index.html. Ninja's internationalization feature, denoted as i18n above, automatically inserts the value from the message file.

Model values and controllers

Now let's try a more interesting way to get values into the template pages, by setting them in the model.

First, we'll add the following line to our index.ftl.html page:

Favorite Beatle: ${simplePojo.favBeatle}

Next, we need to set the simplePojo.favBeatle value. We do that in Ninja's routes file,, which defines all the handlers for each URL. Note that we're able to define our routes in pure Java, with no XML or JSON config at all.

In, you'll see the root URL of the application is configured with a controller. Listing 3 has the relevant line. Note that in Eclipse, you can quickly find this file with ctrl-shift-t, and the start type "Routes."

Listing 3. A Ninja route

router.GET().route("/").with(ApplicationController.class, "index");

Let's unpack this line:

  1. Router.GET means that we are talking about GET requests (i.e., the HTTP GET method).
  2. .route("/") denotes the root URL.
  3. .with(ApplicationController.class, "index") simply says (via a bit of behind-the-scenes reflection): when the URL is matched, send the request to the class (first parameter) using the method (second method).

Next we need to take a look at the ApplicationController.index() method, because that is what will be called to set up the model for the view. Remember, we are making sure that simplePojo.favBeatle will resolve to our preferred Englishman.

Listing 4 shows how this is done.

Listing 4. ApplicationController.index() updated

public Result index() {

        return Results.html();


// …

public static class SimplePojo {

        public String content;


To get our new index page working, just make it looks like this:

Listing 5. A Ninja route

public Result index() {

    	SimplePojo simplePojo = new SimplePojo();

    	simplePojo.favBeatle = "George";

        return Results.html().render(simplePojo);


// ...

public static class SimplePojo {

        public String content;

        public String favBeatle;



Previous Page  1  2  3  4  5  6  7  8  9  10  Next Page 

Sign up for CIO Asia eNewsletters.