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.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

	<context:annotation-config />

	<context:component-scan base-package="com.code2java" />

	<bean id="jspViewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="viewClass"
			value="org.springframework.web.servlet.view.JstlView" />
		<property name="prefix" value="/WEB-INF/view/" />
		<property name="suffix" value=".jsp" />
	</bean>

</beans>

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

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">
	<display-name>DataTableWithSpring</display-name>
	<servlet>
		<servlet-name>dispatcher</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>dispatcher</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>

	<welcome-file-list>
		<welcome-file>/WEB-INF/view/welcome.jsp</welcome-file>
	</welcome-file-list>
</web-app>

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.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>code2java - Datatables with Spring example</title>
</head>
<body>
	<label>Welcome to code2java.com</label>
	<br>
	<!-- redirecting to Spring Controller to load the Data table page -->
	<a href="loadDatatable.do">Load the Datatable</a>
</body>
</html>

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.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>code2java - My Datatable</title>

<!-- Below are the Style Sheets required for Data Tables. These can be customized as required -->
<link rel="stylesheet" type="text/css" href="css/db_site_ui.css">
<link rel="stylesheet" type="text/css" href="css/demo_table_jui.css">
<link rel="stylesheet" type="text/css" href="css/jquery-ui.css">

<!-- Below is the jQuery file required for using any Jquery component. -->
<script type="text/javascript" src="js/jquery-1.11.3.js"></script>

<!-- Below are the jQuery scripts required for Data Tables. -->
<script type="text/javascript" src="js/jquery.dataTables.min.js"></script>

<!-- Initialization code of data table at the time of page load. -->
<script>
	$(document).ready(function() {
		$('#myDatatable').DataTable({
			"jQueryUI" : true,
			"pagingType" : "full_numbers",
			"lengthMenu" : [ [ 5, 10, 50, -1 ], [ 5, 10, 50, "All" ] ]
		/* few more options are available to use */
		});
	});
</script>
</head>
<body>
	<h1>www.code2java.com</h1>
	<h3>The Datatable is loaded.</h3>
	<div style="width: 80%">
		<!-- Below table will be displayed as Data table -->
		<table id="myDatatable" class="display datatable">
			<thead>
				<tr>
					<th>Sr. No.</th>
					<th>Language</th>
					<th>Version</th>
				</tr>
			</thead>
			<tbody>
				<!-- Iterating over the list sent from Controller -->
				<c:forEach var="list" items="${model}">
					<tr>
						<td>${list.srNo}</td>
						<td>${list.language}</td>
						<td>${list.version}</td>
					</tr>
				</c:forEach>
			</tbody>
		</table>
	</div>
</body>
</html>

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

package com.code2java.controllers;

/**
 * @author code2java
 */
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.code2java.models.LanguageModel;


@Controller
public class DTController {
	
	/**
	 * @author code2java
	 * This method will be called when user clicks on link showed on welcome.jsp page
	 * The request is mapped using @RequestMapping annotation. 
	 * @return
	 */
	@RequestMapping(value = "/loadDatatable")
	public ModelAndView loadDatatable()
	{
		List<LanguageModel> model = new ArrayList<LanguageModel>();
		model.add(new LanguageModel(1, "Java", "1.0"));
		model.add(new LanguageModel(2, "Java", "1.1"));
		model.add(new LanguageModel(3, "Java", "1.2"));
		model.add(new LanguageModel(4, "Java", "1.3"));
		model.add(new LanguageModel(5, "Java", "1.4"));
		model.add(new LanguageModel(6, "Java", "1.5"));
		model.add(new LanguageModel(7, "Java", "1.6"));
		model.add(new LanguageModel(8, "Java", "1.7"));
		model.add(new LanguageModel(9, "Java", "1.8"));
		model.add(new LanguageModel(10, "CSS", "1.0"));
		model.add(new LanguageModel(11, "CSS", "2.0"));
		model.add(new LanguageModel(12, "CSS", "3.0"));
		model.add(new LanguageModel(13, "HTML", "1"));
		model.add(new LanguageModel(14, "HTML", "2"));
		model.add(new LanguageModel(15, "HTML", "3"));
		model.add(new LanguageModel(16, "HTML", "4"));
		model.add(new LanguageModel(17, "HTML", "5"));
		
		/* 
		 * 1. view : myDatatable 
		 * 2. model - to be accessed on JSP
		 * 3. model - Object to be sent to JSP page.
		 * */
		return new ModelAndView("myDatatable","model",model);
		
	}

}

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.

Related Posts

  • Abstract Class In JAVA

    Hello Friends, This tutorial is for all the Java followers. One of the best feature that is widely used is the term ‘Abstract’. This term can be used as either class or a simple method. An abstract method is any method that is just declared but not instantiated. In other words one can just create…

  • Threads in Java.

    Hello Friends, This is the tutorial for the java developers. One of the most significance feature of core java is Threading. Threading deals with the processing of Threads in a single java program. Let us learn what actually are Threads. *What are Threads? Threads are independently running processes that are isolated from each other upto…

  • Collections In Java.

    Hello friends, Welcome to another tutorial for java followers. You all may have heard about Collections, it is one of the amazing feature in java. Collections are the object for the group of elements, these elements are nothing but the different data structures like as Array Lists, Linked Lists, Vectors, Hash tables,Hash List, Trees, Hash…

  • Maven Installation.

    Hello Friends, This is one of my tutorial for installation of Maven on windows. Maven is a software project management 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. Maven is an open source community and is a…

  • Jira Plugin Deployment.

    Hello Friends, This is one of the most important tutorial for all the jira developers. You may have heard about the plugins in jira which is a simple Java Archive File with .jar extension. This JAR file contains the class files and auxiliary resources associated with the applications. In Jira we can use Eclipse IDE…

3 Comments

    1. 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

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.