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
@Inject 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
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
AssetsController class. This controller has already been mapped for us in the Maven archetype-generated project. You can see it by looking at
Routes.java, back in Listing 4, where the first two lines display the built-in
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
Sign up for CIO Asia eNewsletters.