Subscribe / Unsubscribe Enewsletters | Login | Register

Pencil Banner

Jump into Java microframeworks, Part 3: Spark

Matthew Tyson | Jan. 8, 2016
An extra lightweight, flexible, and scalable architecture for single-page web apps.

Listing 10. Person model



package org.mtyson.model;

import org.boon.json.annotations.JsonProperty;

public class Person {

    Long id;

	@JsonProperty("first_name")

	private String firstName;

	@JsonProperty("last_name")

	private String lastName;

	public Long getId() {

		return id;

	}

	public void setId(Long id) {

		this.id = id;

	}

	public String getFirstName() {

		return firstName;

	}

	public void setFirstName(String firstName) {

		this.firstName = firstName;

	}

	public String getLastName() {

		return lastName;

	}

	public void setLastName(String lastName) {

		this.lastName = lastName;

	}

}



The @JsonProperty annotation in Listing 10 tells Boon to convert JSON's underscore format (which corresponds to HTML document fields) to the camel-cased fields of a Java class. If you're familiar with Jackson, you'll observe that Boon has borrowed some of its annotations. Also notice the modified addPerson() method on the controller below. It shows how the JSON String is converted in the object.

Listing 11. JSON-to-Java Person instance conversion



public String addPerson(String json){

		Person person = mapper.fromJson(json, Person.class); // Here's where we get our Person instance

		if (dao.addPerson(person)){

			return "{\"message\":\"Added a person!\"}"; 

		} else {

			return "{\"message\":\"Failed to add a person\"}";

		}

	}



In this case we aren't doing anything but persisting the Person object, but we can now use the model instance in whatever business logic we please. In Listing 12 I've updated the JdbcDAO.addPerson() method to use the Person class. The difference here is that the first and last names are now pulled from the Person getters, rather than from the Map used in Listing 7.

Listing 12. JdbcDAO with Person class



public boolean addPerson(Person person) {

    	QueryRunner run = new QueryRunner( dataSource );

		try	{

		    int inserts = run.update( "INSERT INTO Person (first_name, last_name) VALUES (?,?)", person.getFirstName(), person.getLastName());

		} catch(SQLException sqle) {

		    throw new RuntimeException("Problem updating", sqle);

		}

		return true;    	

    }



The Person application's request processing infrastructure now consists of three layers, successively converting request data from JSON, to a Map, to a Java class.

Developing the Spark UI

We have our model and a way to persist it. Next we'll begin developing a UI to save and view objects in the database. In Spark, this means adding static JavaScript resources to use in the template.html page.

To start, create a src/main/resources/public folder to hold the new resources, as shown in Figure 2.

javamicroframeworksp3 fig2
Figure 2. Adding src/main/resources/public to the Eclipse project. Credit: 
Matthew Tyson

Integrating jQuery

For our JavaScript tool we'll use jQuery, which is especially useful for Ajax and DOM handling. If you don't have it already, download the latest version of jQuery (2.1.4 as of this writing) and place it in your new public folder. Another option is to create a file in public and copy the jQuery source into it, or you can actually download the file and copy it into the directory.

 

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

Sign up for CIO Asia eNewsletters.