In Listing 5 we've instantiated a
SimplePojo class, setting its
favBeatle member and then rendering a response with the
simplePojo instance, which is exposed to the view template. (Note that
favBeatle is a public member. I'm sure some purists would frown on that design decision, but it does help us avoid getter/setter resolution kruft.)
This is fairly similar to old fashioned JavaServer Pages and Expression Language template variable resolution.
simplePojo is just a value object used to ferry objects to the view. Since we are in dev mode, we can just reload the browser page and view our musical choice.
Notes about Ninja
You've had a quick look at Ninja, and we'll go much more in-depth with this framework in the next article in this series. For now, just note that Ninja is a stateless architecture. All session information is intended to be stored client-side, and the framework has support for that. This in principle means that scaling is as simple as adding more Ninja nodes to your cluster, without concern for sticky sessions.
For this quick demo, we kind of backed into Ninja's request processing architecture, which consists of routes, views, and controllers. We started at the view and moved into routing and controllers. This pattern is repeated in the other frameworks. As I mentioned earlier, I call these types of frameworks RVC frameworks -- the routes, views, and controllers architecture being their core commonality.
Spark 2: Extremely lightweight RVC for Java 8 and up
Now let's turn our attention to Spark. Not to be confused with Apache's big data processing engine, this Spark is the most lightweight of our three RVC frameworks. It draws inspiration from the Ruby framework, Sinatra.
We'll use Spark 2, the latest version as of this writing, for our demo. Spark 2 doesn't support any Java versions below Java 8, so be sure to install the most recent Java update if you want to follow along. It might seem extreme to drop support for Java 7 but it does keep the project very tight. Moreover, Java 8 is a real winner, and it's great to see how Spark integrates some of its standout features. We'll explore Spark's support for lambda expressions when the time comes.
For now, just as we did with Ninja, start by creating a new Maven project, as shown in Listing 6. In this case, there is no Spark specific archetype, so we'll just use a generic Maven archetype.
Listing 6. Creating a new Spark project
mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
cd into our freshly minted project
dir and add Listing 7 to your
pom.xml using a text editor.
Sign up for CIO Asia eNewsletters.