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

Listing 14. Play's Scala view template

@(message: String)

@main("Welcome to Play") {

    @play20.welcome(message, style = "Java")


It's important to note that the Scala template actually compiles into a Scala class. That is why we executed the template as a method call earlier.

Scala view resolution in Eclipse

To get Eclipse to resolve the compiled template classes properly, I added the target.scala-2.11.classes folder as a project dependency.

Here's some detail about the Scala template syntax in Listing 14:

  1. @(message: String) declares an argument, called message, of type string.
  2. @main("Welcome to Play") { references the "main" template, passing in a String argument.
  3. @play20.welcome(message, style = "Java") passes in a second argument to the main template. This argument is a call to another template, the built-in play20.welcome template, passing in two arguments.

A look at the main.scala.html template will show a very simple HTML layout that displays the first message parameter and the second template parameter (String and HTML types, respectively).

A model in Play

To wrap up our first look at Play, let's expose a model in the view. We'll create a new class called models.Group, which we'll use to model a musical group. Put the new class in /app/models. Give it a private String member name, with getters and setters and a constructor that takes the member, as seen in Listing 15. We can make this class a persistent class later on, but for now it's just a POJO.

Listing 15. The models.Group Class

package models;
public class Group {
	String name;
	public Group(String name) {
		super(); = name;
	public String getName() {
		return name;
	public void setName(String name) { = name;

Now jump back into and update the index() method as shown in Listing 16.

Listing 16. Passing a model to the view

public Result index() {
    	List<Group> groups = new ArrayList<Group>();
    	groups.add(new Group("Bob Marley and the Wailers"));
    	groups.add(new Group("Eric Clapton Solo"));
    	groups.add(new Group("George Harrison Solo"));
    	groups.add(new Group("Travelling Wilburys"));
    	groups.add(new Group("Tom Petty and the Heartbreakers"));
		groups.add(new Group("Fleetwood Mac"));
		groups.add(new Group("The Eagles"));
        return ok(views.html.index.render("Your new application is ready.",groups));

Listing 16 gives us the seven degrees of separation between Bob Marley and The Eagles (meaning that each band is related to the next somehow), in the form of a List, which is passed into the view-render method as a second argument. Next, we need to update the index view (index.scala.html) to accept another argument and do something with it. Listing 17 shows that.

Listing 17. Updating index.scala.html

@(message: String)(groups: List[Group])

@main("Welcome to Play") {




@for(g <- groups) {





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

Sign up for CIO Asia eNewsletters.