Struts pagination DataTables

Struts pagination DataTables

This demo you will see how to create pagination using DataTables for Struts 2 application. DataTable is plug-in basically for jQuery. For more information about DataTables please go here:

Tools needed:

  • Eclipse 3.4 or above (Download from here) – Note for this demo we have used eclipse Kepler
  • JDK 1.5 or above (Download from here )
  • Tomcat 6 or above (Please follow link to install and configure tomcat in eclipse: Configure and Run Tomcat server in eclipse )
  • Maven 3.0.4
  • struts2-json-plugin (Dependency already included in pom.xml)
  • DataTables CSS,JS which you can get it directly form its site here (Links are already included in jsp page you don’t need to download anything)
  • JQuery js file (Links are already included in jsp page you don’t need to download anything)

Final Struts 2 pagination screen you will see after all configuration as below: 

Struts pagination DataTables

Steps:

  • Create maven project name: Struts2PaginationDatatables
  • Final project structure:

Struts pagination DataTables

  • pom.xml file dependency:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.javahonk</groupId>
	<artifactId>Struts2PaginationDatatables</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>Struts2PaginationDatatables Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-core</artifactId>
			<version>2.3.16.3</version>
		</dependency>
		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-convention-plugin</artifactId>
			<version>2.3.16.3</version>
		</dependency>
		
		<!-- JASON Plug-in -->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-json-plugin</artifactId>
            <version>2.3.16.3</version>
        </dependency>
	</dependencies>
	<build>
		<finalName>Struts2PaginationDatatables</finalName>
	</build>
</project>
  • web.xml
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
	<display-name>Archetype Created Web Application</display-name>

	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
		<init-param>
			<param-name>struts.devMode</param-name>
			<param-value>true</param-value>
		</init-param>						
	</filter>

	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
</web-app>
  • index.jsp:
<%@ 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">
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Struts 2 pagination using DataTables</title>
<link rel="stylesheet" type="text/css" href="//cdn.datatables.net/1.10.0/css/jquery.dataTables.css">
<script type="text/javascript" src="//code.jquery.com/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="//cdn.datatables.net/1.10.0/js/jquery.dataTables.js"></script>
<script type="text/javascript">

$(document).ready(function() {
     
    $("#example").dataTable( {
        "bProcessing": false,
        "bServerSide": false,
        "sort": "position",
        "sAjaxSource": "strutsPaginationAction",
        "aoColumns": [
            { "mData": "name" },
            { "mData": "position" },
            { "mData": "office" },
            { "mData": "phone" },
            { "mData": "start_date" },
            { "mData": "salary" },
             
        ]
    } );

} );

</script>
</head>
<body>
<h2 >Struts 2 pagination using DataTables<br><br></h2>
<table width="70%" style="border: 3px;background: rgb(243, 244, 248);"><tr><td>
    <table id="example" class="display" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th>Name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Phone</th>
                <th>Start Date</th>
                <th>Salary</th>
            </tr>
        </thead>       
    </table>
    </td></tr></table>
</body>
</html>
  • StrutsPaginationAction.Java
package com.javahonk.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;

import com.opensymphony.xwork2.ActionSupport;

@ParentPackage("json-default")
public class StrutsPaginationAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private int iTotalRecords;
	private int iTotalDisplayRecords;
	private String sEcho;
	private String sColumns;
	private List<Person> aaData;
	private List<Person> personsList = new ArrayList<Person>();

	public StrutsPaginationAction() {

		for (int i = 0; i < 1; i++) {
			Person person2 = new Person();
			person2.setName("John Landy");
			person2.setPosition("System Architect");
			person2.setSalary("$320,800");
			person2.setOffice("NY");
			person2.setPhone("999999999");
			person2.setStart_date("05/05/2010");
			personsList.add(person2);

			person2 = new Person();
			person2.setName("Igor Vornovitsky");
			person2.setPosition("Solution Architect");
			person2.setSalary("$340,800");
			person2.setOffice("NY");
			person2.setPhone("987897899");
			person2.setStart_date("05/05/2010");
			personsList.add(person2);

			person2 = new Person();
			person2.setName("Java Honk");
			person2.setPosition("Architect");
			person2.setSalary("$380,800");
			person2.setOffice("NY");
			person2.setPhone("1234567890");
			person2.setStart_date("05/05/2010");
			personsList.add(person2);

			person2 = new Person();
			person2.setName("Ramesh Arrepu");
			person2.setPosition("Sr. Architect");
			person2.setSalary("$310,800");
			person2.setOffice("NY");
			person2.setPhone("4654321234");
			person2.setStart_date("05/05/2010");
			personsList.add(person2);

			person2 = new Person();
			person2.setName("Bob Sidebottom");
			person2.setPosition("Architect");
			person2.setSalary("$300,800");
			person2.setOffice("NJ");
			person2.setPhone("9876543212");
			person2.setStart_date("05/05/2010");
			personsList.add(person2);

		}

		for (int i = 0; i < 50; i++) {
			Person person2 = new Person();
			person2.setName("Zuke Torres");
			person2.setPosition("System Architect");
			person2.setSalary("$320,800");
			person2.setOffice("NY");
			person2.setPhone("999999999");
			person2.setStart_date("05/05/2010");
			personsList.add(person2);
		}

		setiTotalDisplayRecords(personsList.size());
		setiTotalRecords(personsList.size());
		setAaData(personsList);

	}

	@Override
	@Action(value = "/strutsPaginationAction", results = { @Result(name = "success", type = "json") })
	public String execute() throws Exception {
		return ActionSupport.SUCCESS;
	}

	public List<Person> getPersonsList() {
		return personsList;
	}

	public void setPersonsList(List<Person> personsList) {
		this.personsList = personsList;
	}

	public int getiTotalRecords() {
		return iTotalRecords;
	}

	public void setiTotalRecords(int iTotalRecords) {
		this.iTotalRecords = iTotalRecords;
	}

	public int getiTotalDisplayRecords() {
		return iTotalDisplayRecords;
	}

	public void setiTotalDisplayRecords(int iTotalDisplayRecords) {
		this.iTotalDisplayRecords = iTotalDisplayRecords;
	}

	public String getsEcho() {
		return sEcho;
	}

	public void setsEcho(String sEcho) {
		this.sEcho = sEcho;
	}

	public String getsColumns() {
		return sColumns;
	}

	public void setsColumns(String sColumns) {
		this.sColumns = sColumns;
	}

	public List<Person> getAaData() {
		return aaData;
	}

	public void setAaData(List<Person> aaData) {
		this.aaData = personsList;
	}

}
  • PersonJsonObject.java
package com.javahonk.action;

import java.util.List;

public class PersonJsonObject {
	
	private int iTotalRecords;

	private int iTotalDisplayRecords;

	private String sEcho;

	private String sColumns;

	private List<Person> aaData;

	public int getiTotalRecords() {
		return iTotalRecords;
	}

	public void setiTotalRecords(int iTotalRecords) {
		this.iTotalRecords = iTotalRecords;
	}

	public int getiTotalDisplayRecords() {
		return iTotalDisplayRecords;
	}

	public void setiTotalDisplayRecords(int iTotalDisplayRecords) {
		this.iTotalDisplayRecords = iTotalDisplayRecords;
	}

	public String getsEcho() {
		return sEcho;
	}

	public void setsEcho(String sEcho) {
		this.sEcho = sEcho;
	}

	public String getsColumns() {
		return sColumns;
	}

	public void setsColumns(String sColumns) {
		this.sColumns = sColumns;
	}

	public List<Person> getAaData() {
		return aaData;
	}

	public void setAaData(List<Person> aaData) {
		this.aaData = aaData;
	}
}
  • Person.java
package com.javahonk.action;

public class Person {
	private String name;
	private String position;
	private String office;
	private String phone;
	private String start_date;
	private String salary;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPosition() {
		return position;
	}

	public void setPosition(String position) {
		this.position = position;
	}

	public String getOffice() {
		return office;
	}

	public void setOffice(String office) {
		this.office = office;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getStart_date() {
		return start_date;
	}

	public void setStart_date(String start_date) {
		this.start_date = start_date;
	}

	public String getSalary() {
		return salary;
	}

	public void setSalary(String salary) {
		this.salary = salary;
	}
}
  • Run this set up in tomcat server. If you didn’t configure tomcat set up in eclipse yet please use this tutorial: Configure and Run Tomcat server in eclipse. Now right click project –>Run As –> Run on server you will see below pagination screen:

Struts pagination DataTables

  •  Search filter with name:

Struts 2 pagination DataTables

download Download Project:  Struts2PaginationDatatables

That’s it Struts 2 pagination DataTables

4 thoughts on “Struts pagination DataTables”

Leave a Reply

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