Pagination in Struts 2 using jQuery datatable

In this Struts 2 tutorial, we will organize the bunch of data into pagination. In this example, we are using jQuery dataTable to create pagination for a huge amount of data. dataTable hold all the data at DOM level and show the required data one by one.

JS Snippet
$(document).ready(function() {

In this example, we are total fetching 500 records from the database and show only 10 record at a time. Add the below links in your JSP page to work properly.

<link rel="stylesheet" href="">
<link rel="stylesheet" type="text/css" href="">
<script src=""></script>
<script type="text/javascript" charset="utf8" src=""></script>

Similar tutorial- Struts 2 pagination using display tag


DataTabale help you in the followings…

  • Pagination
  • Instant searching
  • Multi-column ordering
  • Dynamic creation of tables
  • Custom DOM positioning

Project Structure in Eclipse

Pagination in Struts 2 using jQuery datatable

Software and Library Used

  1. Eclipse IDE
  2. Tomcat 8 Server
  3. Struts2.3.x. jars
  4. jQuery library
  5. DataTable library
  6. Bootstrap library

Define the Struts 2 filter

Before staring the code we need define struts 2 filter in web.xml


Create Data Access Class and Bean Class

Create the DAO and Bean class that communicate with data base and retrieve the data from table.
package org.websparrow;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class PaginationDAO {
	public Connection connect() throws Exception {
		return (Connection) DriverManager.getConnection("jdbc:mysql://", "root", "");

	public ResultSet pageList() {
		ResultSet rs = null;
		try {
			String query = "SELECT city_id,city_name,city_state FROM cities LIMIT 500";
			Statement stmt = connect().createStatement();
			rs = stmt.executeQuery(query);
		} catch (Exception e) {
			return null;
		return rs;
package org.websparrow;

public class PaginationBean {
	// generate getters and setters...
	private String city_id, city_name, city_state;

Create Action Class
package org.websparrow;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

public class PaginationAction extends ActionSupport {

	private static final long serialVersionUID = -4215116840067378030L;

	private List<PaginationBean> pageBean = null;

	public ResultSet rs = null;
	public PaginationDAO pageDao = new PaginationDAO();

	public String execute() throws Exception {

		try {
			setPageBean(new ArrayList<PaginationBean>());
			PaginationBean bean = null;
			rs = new PaginationDAO().pageList();

			if (rs != null) {
				while ( {

					bean = new PaginationBean();
		} catch (Exception e) {

		return "SUCCESS";

	public List<PaginationBean> getPageBean() {
		return pageBean;

	public void setPageBean(List<PaginationBean> pageBean) {
		this.pageBean = pageBean;

JSP Page

Now create the page that interact with user.

<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html>
<link rel="stylesheet" href="">
<link rel="stylesheet" type="text/css" href="">
<script src=""></script>
<script type="text/javascript" charset="utf8" src=""></script>
	$(document).ready(function() {
	<div class="container">
		<h1>Pagination in Struts 2 using jQuery datatable example</h1>
		<s:form action="pagination.action" method="post">
			<s:submit cssClass="btn btn-lg btn-primary"></s:submit>

		<div style="margin-top: 50px; margin-right: 282px;">
			<table class="table table-hover table-bordered" id="mytbl">
					<tr class="success">
						<th>City Id</th>
						<th>City Name</th>
						<th>City State</th>
				<s:iterator value="pageBean">
							<s:property value="city_id" />
							<s:property value="city_name" />
							<s:property value="city_state" />

Map the Action and Result

Finally define the action and result in struts.xml

	<constant name="struts.devMode" value="true" />
	<package name="default" extends="struts-default" namespace="/">
		<action name="pagination" class="org.websparrow.PaginationAction">
			<result name="SUCCESS">/pagination.jsp</result>


Now run your application and hit the SUBMIT button.

Pagination in Struts 2 using jQuery datatable

Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.