Subscribe / Unsubscribe Enewsletters | Login | Register

Pencil Banner

Jump into Java microframeworks, Part 4: Play

Matthew Tyson | March 18, 2016
A closer look at Java's original, ultra-lightweight web development stack

Queries in Play

So, we can create groups and save them to the database -- success! Now let's see how we could render out the existing groups. We'll start by adding a route, as shown in Listing 18.

Listing Listing 18. loadGroups route

GET    /groups						controllers.Application.loadGroups()

Our loadGroups handler shows how you can utilize the EBean query API:

Listing Listing 19. EBean query API in loadGroups()

import com.avaje.ebean.Ebean;
import play.libs.Json;
public Result loadGroups() {
    	List<Group> groups = Ebean.find(models.Group.class).findList();
    	return ok(Json.toJson(groups).toString());

In Listing 19, Ebean exposes a simple find method for querying entities by class type. We can then take the List and convert it to JSON, with Play's built-in JSON library. If you hit http://localhost:9000/groups with a GET request, you'll now see a JSON-formatted list of the existing groups. Before we move on, let's explore another way to handle querying in Play.

Another query pattern

Adding a static Finder member to the Group class is a common querying pattern in Play, as shown in Listing 20.

Listing Listing 20. static Finder member on Group class

public static Finder<String, Group> find = new Finder<String, Group>(Group.class);

Returning to the loadGroups() method on the application controller, we would change it to look like Listing 21.

Listing Listing 21. Application.loadGroups()

public Result loadGroups() {
    	//List<Group> groups = Ebean.find(models.Group.class).findList();
    	List<Group> groups = Group.find.all();
    	return ok(Json.toJson(groups).toString());

Now we are set up to use the static Finder to access the list of groups.

Complete the user interface

It's now a fairly simple exercise to render the groups to the UI. First, add a div to the index.html.scala page, like so: <div id="groups"></div>. Next. make the additions from Listing 22 to your hello.js.

Listing Listing 22. loadGroups additions to hello.js

App = {
		startup: function(){
		loadGroups: function(){
			$.getJSON( "/groups", function( data ) {
				  var items = [];
				  $.each( data, function( key, val ) {
				    items.push( "<li id='" + key + "'>" + + "</li>" );

When the index page is first opened, the loadGroups method will execute, and some basic jQuery will generate an HTML list. That list will display the results of the JSON-formatted data from the GET /groups service. Whenever a group is added, the list will be refreshed to reflect the changes.

Nothing can stop us now -- we can save and list everything from ABBA to Zappa!


Between the setup in Part 1 and the simple programming exercise in this article, you should have a pretty good grasp of incorporating persistence, assets, views, and JSON in your Play-based applications. Play offers a lot of built-in functionality, and this tour gave us a sense of how things work in the Play idiom. Overall, Play is the most intensive of the Java microframeworks to get into, offering a full-stack alternative to standard Java tooling.


Previous Page  1  2  3  4  5  6  7  Next Page 

Sign up for CIO Asia eNewsletters.