DataTables Column Render

Today I’ll add an actions column to the DataTable. The column will contain an edit icon that will open up a dialog displaying the row parameters for update. I’ll keep this post to a reasonable length by just displaying an alert on submit rather than connecting to the back end.

First, I’ll add support for the Font Awesome font and CSS Toolkit. This will let me display an icon as the anchor text for the new actions column.

Read More

Well Rounded

It has always been my choice to focus on the server side rather than the client side. But my recent exploration into Bootstrap is really turning things around. The Bootstrap grid takes the complexity out of html layout. And the success of Bootstrap has created a market for Bootstrap Themes at {wrap}bootstrap, Start Bootstrap, Creative Market, et al. It is absolutely possible to create a good looking, mobile ready web application without an advanced degree in browser specific behavior.

I used to say I was full-stack because I could build a functional web application in basic html and css. But I wouldn’t have been proud of the front-end. With Bootstrap and Datatables in my toolkit, I don’t need to apologize for my front-end.

Modal Dialog for Jersey, Gson and DataTables

Today, I will add a modal dialog to add new users.

First, the table presentation is updated to use the appropriate bootstrap table presentation. The base table class defines light padding with horizontal dividers. I add the table-striped class to add zebra-striping and the table-bordered class to add borders on all sides of the table and cells.

1
2
3
4
5
6
7
8
9
@@ -20,7 +20,7 @@
<div class="row">
<div class="col-md-8">
<h2>DataTable with ajax object feed</h2>
- <table id="users" class="display">
+ <table id="users" class="table table-striped table-bordered">
<thead>
<tr>
<th>email</th>

Read More

Jersey, Gson and DataTables

With all the backend pieces in place, I’ll add a jQuery DataTable to display the data. This only requires the creation of an html table and a little bit of javascript.

First, I pull in the prerequisite assets from Bootstrap, jQuery and jQuery DataTables (Bootstrap is, of course, completely optional).

1
2
3
4
5
6
7
8
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"/>
<link rel="stylesheet" href="//cdn.datatables.net/1.10.2/css/jquery.dataTables.min.css"/>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css"/>
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="//cdn.datatables.net/1.10.2/js/jquery.dataTables.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>

Read More

Custom marshalling with Gson - part 2

My original motivation for this series of posts was the “new” 1.10 release of jQuery DataTables. This release added client-side processing with data delivery as json objects via ajax — a great match for a jersey back end. The Ajax data source (objects) requires that the data be provided with a key of “data” and a value of an array of objects. I’ll update our project to provide the UserMap in that format.

Here’s the UserMapMarshall class implementing JsonSerializer. First it creates an array of json objects from the UserMap values and then it creates a json object with a key of “data” and a value of the array. The Gson object is thread-safe, so it’s safe to retain as a static member.

Read More

Custom marshalling with Gson - part 1

Today, I’ll extend our project to add temporary storage of uploaded users in a HashMap<String,User> and allow retrieval of that HashMap as json. First, the new UserMap class. This class will be needed to register custom marshalling.

1
2
3
4
5
6
7
package com.ideoplex.tutorial;
import java.util.concurrent.ConcurrentHashMap;
public class UserMap extends ConcurrentHashMap<String,User> {
}

And three changes to the MyResource class. First, a static UserMap member to store the uploaded users. Second, a new getUserMap method to return the static member. And finally, an update to the postUser method to throw a WebApplicationException when uploading a duplicate email address and add the user to the static UserMap otherwise.

Read More

Jersey and Gson

Now that I have the project legwork out of the way, I can add Gson support to our web application. Our first step is adding gson as a project dependency. This will direct maven to include gson and its dependencies in the project.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ git diff
diff --git a/pom.xml b/pom.xml
index 0fe5b04..eedf81b 100644
--- a/pom.xml
+++ b/pom.xml
@@ -70,6 +70,11 @@
<artifactId>jersey-media-moxy</artifactId>
</dependency>
-->
+ <dependency>
+ <groupId>com.google.code.gson</groupId>
+ <artifactId>gson</artifactId>
+ <version>2.3</version>
+ </dependency>
</dependencies>
<properties>
<jersey.version>2.12</jersey.version>

Next, I’ll pull in some code from REST with Java in practice by Lars Michael. These two classes implement the javax.ws.rs.ext.MesssageBodyWriter and javax.ws.rs.ext.MessageBodyReader interfaces necessary to marshall objects to and from json. The @Provider annotation allows JAX-RS autodiscovery to automatically find and install our classes. First, the GsonWriter class:

Read More

maven lifecycle and plugins

Last week I used the mvn tomcat7:redeploy command to Deploy to Tomcat with maven. This command literally invokes the redeploy action of the tomcat7 plugin. Today, I’ll tie the tomcat7 plugin into the maven lifecycle.

The maven default build lifecycle has eight phases:

  • validate
  • compile
  • test
  • package
  • integration-test
  • verify
  • install
  • deploy

I’ll bind the tomcat7:redeploy action to the install lifecycle phase.

Read More

Deploy to Tomcat with maven

Previously, I used maven to generate and package a jersey webapp. Today, I’ll extend our project to deploy the war file to Tomcat. This basically amounts to configuring Tomcat to accept connections from maven and configuring maven to use a specific Tomcat instance.

For our sample project, I’ll use a Tomcat instance on Nitrous.io. If you’re also using Nitrous, then you’ll probably need to start by installing tomcat on your instance:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$ parts install tomcat
=> Extracting archive...
=> Installing...
=> Activating...
=> Installed tomcat 7.0.53
To start the Apache Tomcat server:
$ parts start tomcat
To stop the Apache Tomcat server:
$ parts stop tomcat
Apache Tomcat config is located at:
$ /home/action/.parts/packages/tomcat/7.0.53/conf
Default webapps at:
$ /home/action/.parts/packages/tomcat/7.0.53/webapps
Default port: 8080

Read More

Getting started with Maven

Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project’s build, reporting and documentation from a central piece of information.

Ask ten developers about maven and you’ll probably get eleven opinions. The more experienced developers are apt to be on their 3rd or 4th opinion. My opinion of maven is similar to Churchill’s opinion of democracy:

democracy is the worst form of Government except for all those other forms that have been tried …

Churchill

Maven does everything — it will generate a template, download dependencies, build the project, run the tests and deploy the project. Let’s see how easy it is to build a jersey web application with maven.

Read More