Build a RESTful application using jqGrid

This tutorial shows how you can build up an Enterprise application using jQuery and jqGrid plugin on the client side and REST Web services deployed on WildFly application server.

Let’s start building the server side at first. Our SimpleRESTService contains two methods only: getPersonList which retrieves a list of Person Entity using a JPA Query and controller method that is invoked for create/delete/update operations.

public class SimpleRESTService {

    EntityManager em;

    public List<Person> getPersonList()


        Query query = em.createQuery("FROM Person");
        List<Person> customerList = query.getResultList();
        return customerList;

    public void controller(String message) {
        JsonReader reader = Json.createReader(new StringReader(message));
        JsonObject obj = reader.readObject();

        String oper = obj.getString("oper");
        switch (oper) {
        case "add":
        case "del":

    private void editElement(JsonObject obj) {
        String id = obj.getString("id");
        String name = obj.getString("name");
        String surname = obj.getString("surname");
        String address = obj.getString("address");

        Person person = em.find(Person.class, new Long(id));



    private void addElement(JsonObject obj) {

        Person person = new Person();

        System.out.println("Saved " + person);


    private void deleteElement(JsonObject obj) {

        String id = obj.getString("id");
        Query query = em.createQuery("delete FROM Person where id = :id");
        query.setParameter("id", id);
        System.out.println("Deleted " + obj);


The Entity class is quite simple, containing two fields and one auto-generated id field

public class Person {
    public Person() {     }
    public Person(String name, String surname) { = name;
        this.surname = surname;
    private Long id;
    private String name;
    private String surname;
    public String getName() {
        return name;
    public void setName(String name) { = name;
    public String getSurname() {
        return surname;
    public void setSurname(String surname) {
        this.surname = surname;

Our REST application needs as well a JAX-RS Activator class which enables REST Services setting as base path “rest”:

public class JaxRsActivator extends Application {

Our server application is completed. You need the configuration file for JPA Persistence. To keep it simple, we will be using the default ExampleDS which ships with the application server. This file named persistence.xml needs to be included in the resources/META-INF folder of your project.

<persistence version="2.0"
   xmlns="" xmlns:xsi=""
   <persistence-unit name="primary">
         <!-- Properties for Hibernate -->
         <property name="" value="create-drop" />
         <property name="hibernate.show_sql" value="false" />

Finally, although not mandatory, we will include as well an import.sql file in the resources folder of your Maven application. This will create some initial data that can be displayed:

insert into person (id,name,surname,address) values (0,'freddie','smith','Euston Road')
insert into person (id,name,surname,address) values (1,'joe ','francis','Park Lane')
insert into person (id,name,surname,address) values (2,'paul','stephens','Piccadilly avenue')

Now on the client side. We will be using jQuery and jqGrid. jqGrid is an Ajax-enabled JavaScript control that provides solutions for representing and manipulating tabular data on the web. Since the grid is a client-side solution loading data dynamically through Ajax callbacks, it can be integrated with our server-side REST Service.

<html xmlns="">
<title>jqGrid Tutorial</title>

<link rel="stylesheet" type="text/css" media="screen" href="" />
<link rel="stylesheet" type="text/css" media="screen" href="" />
<script src="//"></script>

<script src="" type="text/javascript"></script>
<script src="" type="text/javascript"></script>
<script type="text/javascript">
        jQuery.jgrid.edit, {
            ajaxEditOptions: { contentType: "application/json" },
            recreateForm: true,
            serializeEditData: function(postData) {
                return JSON.stringify(postData);
            afterSubmit: function (response, postdata) {
                var res = jQuery.parseJSON(response.responseText);
                return [true, "", res.d];

        function() {
                                url : 'http://localhost:8080/rest-angular/rest/service',
                                datatype : 'json',
                                mtype : 'GET',
                                colNames : [ 'name', 'surname',
                                colModel : [ {
                                    name : 'name',
                                    index : 'name',
                                    width : 150,
                                    editable : true
                                }, {
                                    name : 'surname',
                                    index : 'surname',
                                    width : 150,
                                    editable : true
                                }, {
                                    name : 'address',
                                    index : 'address',
                                    width : 200,
                                    editable : true
                                } ],
                                pager : '#pager',
                                rowNum : 10,
                                rowList : [ 10, 20, 30 ],
                                sortname : 'invid',
                                sortorder : 'desc',
                                viewrecords : true,
                                gridview : true,
                                caption : 'Data Report',
                                jsonReader : {
                                    repeatitems : false,
                                editurl : "http://localhost:8080/rest-angular/rest/service/controller",
                                datatype : 'json'
            jQuery("#list").jqGrid('navGrid', '#pager', {
                edit : true,
                add : true,
                del : true,
                search : true
    <table id="list">
            <td />
    <div id="pager"></div>

Basically, an instance of jqGrid is a javascript object, with properties, events and methods. Properties may be strings, numbers, arrays, boolean values or even other objects.
Within this example, data is retrieved from url : ‘http://localhost:8080/rest-angular/rest/service’ which returns data in JSON format. The colNames and colModel define respectively the header contained in the grid and the model which has been returned from JSON. Next, we have defined a set of properties that eventually determine how the items are paged, sorterd and displayed. Finally we have enabled all possible CRUD operations on the grid by setting:

jQuery("#list").jqGrid('navGrid', '#pager', {
                edit : true,
                add : true,
                del : true,
                search : true

Each time that a create/delete/update operation is POSTed to the server, it sent in this JSON format:
{“name”:”joe “,”surname”:”dalton”,”address”:”Park Lane”,”oper”:”edit”,”id”:”2″}
This kind of format works well with a controller on the server side that can handle multiple type of operations with a single REST URL. Here’s the resulting grid in action:

angularjs jqgrid

Adding a new Person:

Editing a Person:

angularjs jqgrid

That’s all! In the next tutorial we will explore some more type of interactions between jqGrid plugin and Java server side applications. Stay tuned!