Datatables with Spring MVC

This tutorial explains the implementation of Datatables with Spring MVC. Most of you all will be aware of usage of data tables in a web application, here we will see how to integrate it with Spring MVC application. This is like any other web page that we create in our MVC application, the only different thing here is using jQuery’s data table to display the data in a tabular form and with additional options like paging, searching and sorting.

Initially we will create a Dynamic Web Project in eclipse, and we will end up with something like this in the project structure.

Project Structure

Project Structure

Step 1: Create a dynamic web project, name it “DataTableWithSpring”

Step 2: Add required libraries in the lib folder. All the required Spring jars are added as shown in below screenshot.

libraries

libraries

Step 3: Create dispatcher-servlet.xml file under WEB-INF folder, which is nothing but the Spring Front Controller which controls the request and response form and to the user.

Step 4: Update the web.xml file, which is most important in any web application, for servlet mapping to be handled.

Step 5: Add welcome file to be displayed on application startup. It is added under /WEB-INF/view folder. On this page, we have a link to go to new page which will display the data table. This link will call the controller method, which will serve the request and load the data table. We need to include all the required CSS and JS files which are required for datatables. Same is bundled in the attached source code at the end of this blog.

Step 6: Create a new jsp page – “myDatatables.jsp” which consists of the actual data to be displayed in data table. We have used JSTL’s forEach loop to iterate over the ‘model’ object sent from controller.

Step 7: We have to create a controller class, DTController.java. This will contain the method that will return the Model and View consisting of model object and view to render, in this case

Step 8: We are done with the implementation. Run your application and output will be something like below:

Output : Welcome Page.

output_welcome_page

output_welcome_page

Output : Datatables Page.

output_datatable_page

output_datatable_page

Hope this will help you.

Download the complete source – DataTableWithSpring

Regards,

Nikhil Naoghare.

3 Comments

  1. kingsleybunni

    Where is the LamguageModel.java file ?
    What is it ? I can’t find it.
    Plaese help

    Reply
    1. admin (Post author)

      You can find it in the download folder under com.models.* package. It is just a POJO class that will hold the values for language

      Reply
  2. utkal

    Thanks a lot Nikhil

    Reply

Leave a Comment

Your email address will not be published. Required fields are marked *