Subscribe / Unsubscribe Enewsletters | Login | Register

Pencil Banner

Jump into Java microframeworks, Part 2: Ninja

Matthew Tyson | Nov. 25, 2015
Build a functional Person application with Ninja.

You can inject the router object that holds getReverseRoute() into your controllers as shown in Listing 8. Just set a member on the Controller class, with the Router type, and use the Guice @Inject annotation. Ninja will then set its own router.

Listing 8. Injecting the router into a controller

Router router;

You'll discover the true power of reverse routing when you need to forward a request to a destination in your application that uses URL and/or query parameters. The redirected route resolves based on the given request parameters, and the resulting handler will receive them just like a normal request. Furthermore, you can use the dispatching method to set these parameters; just use the third parameter of the getReverseRoute() method, like so:

router.getReverseRoute(ApplicationController.class, "index", parameterMap)

In this case, parameterMap is an instance of java.util.Map. Map keys will match to URL parameters if it finds any; otherwise, they'll be treated as query parameters.

Logging and debugging in Ninja

Ninja uses Logback for logging, and you can configure it as normal. Specify a Logback file in application.conf, as shown below. Remember, too, that you can select an environment if you wish, using the percentage notation shown in the "Selecting an environment" sidebar:


You can locate this file in the filesystem or on the classpath. The Maven archetype we originally used to generate the project will have placed a default at /src/main/java.

See the Logback documentation for information about configuring your logging setup.

Debugging with Maven

In order to debug your code, you need to be able to step into it. To set yourself up for debugging with Maven, go into the project and run mvnDebug jetty:run, then attach a remote debugger. In Eclipse, go to Run->Debug Configurations...->Remote Java Applications->New, where you should be able to accept the defaults of Socket Attach, localhost:8000. I'll say more about testing later in the article.

Remember: When in debug mode, the app will not be responsive until you attach the debugger!

Adding a functional UI

Next we'll add a user interface for creating a person. To build a real UI, we'll need control over the application's JavaScript, CSS, and any images or assets that it serves. Ninja gives us this control via the AssetsController class. This controller has already been mapped for us in the Maven archetype-generated project. You can see it by looking at, back in Listing 4, where the first two lines display the built-in Asset routing.

Developers have two options for resolving third-party dependencies in Ninja: straight link or WebJars. I'll demonstrate both, starting with WebJars.

Resolving dependencies with WebJars

WebJars is a JavaScript dependency management solution for Java, similar to Bower or Browserfy but accessible from inside a Java app. The very nice thing about WebJars is that it enables JVM build tools like Maven and Gradle to pull down dependencies for you. This falls into the category of Really Good Ideas.


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

Sign up for CIO Asia eNewsletters.