4:43 am - Tuesday April 25, 2017

Pagination tutorial and example program in Struts 1x

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

 

Pagination is the process of organizing or splitting large amount of data or rows into manageable size and numbering them to view the set of content, when you’re having large number of rows to be displayed in a web page we should organize them to display them as page 1,2,3 etc.

In this struts tutorial we will learn how to create pagination for large data and sort them in ascending or descending order using displaytag.tld file in struts, display tag can handle data to display in ascending , descending order and decoration of a table in a customizable manner.

Displaytag.tld is an open source suit you can down it from http://www.displaytag.org/10/tlds.html

Project  Structure                                             
 

  Jarfiles

 

 Tld Files

Following is the list of required JAR files to be added in Java Class Path of your project. Download displaytag JAR files from

JAR FILES + TLD FILES

EmployeeData.java 


package Com.Candidjava;

import java.util.ArrayList;

public class EmployeeData {

	private String id;
	private String userName;
	private String emailId;

	public EmployeeData(String Id, String userName, String emailId) {
		this.id = Id;
		this.userName = userName;
		this.emailId = emailId;
	}

	public EmployeeData() {
		// TODO Auto-generated constructor stub
	}

	public ArrayList loadData() {
		ArrayList userList = new ArrayList();
		userList.add(new EmployeeData("001", "Mathanlal sait","mathanlal@candidjava.com"));
		userList.add(new EmployeeData("002", "Anburaj","anburaj@candidjava.com"));
		userList.add(new EmployeeData("003", "Vignesh","vignesh@candidjava.com"));
		userList.add(new EmployeeData("004", "Antony", "antony@candidjava.com"));
		userList.add(new EmployeeData("005", "Gopukumar", "gobu@candidjava.com"));
		userList.add(new EmployeeData("006", "Kalaimani","kalaimani@candidjava.com"));
		userList.add(new EmployeeData("007", "karthika","karthika@candidjava.com"));
		userList.add(new EmployeeData("008", "deepa", "deepa@candidjava.com"));
		userList.add(new EmployeeData("009", "Prabhu", "praphu@candidjava.com"));
		userList.add(new EmployeeData("010", "karupia","karupia@candidjava.com"));
		userList.add(new EmployeeData("011", "Bala", "bala@candidjava.com"));
		userList.add(new EmployeeData("012", "Mahesh", "mahesh@candidjava.com"));
		userList.add(new EmployeeData("013", "Rajendran","rajendran@candidjava.com"));
		return userList;
	}

	/**
	 * @return the id
	 */
	public String getId() {
		return id;
	}

	/**
	 * @return the userName
	 */
	public String getUserName() {
		return userName;
	}

	/**
	 * @return the emailId
	 */
	public String getEmailId() {
		return emailId;
	}

}

ApplicationResource.properties


# -- standard errors --
errors.header=<UL>
errors.prefix=<LI>
errors.suffix=</LI>
errors.footer=</UL>
# -- validator --
errors.invalid={0} is invalid.
errors.maxlength={0} can not be greater than {1} characters.
errors.minlength={0} can not be less than {1} characters.
errors.range={0} is not in the range {1} through {2}.
errors.required={0} is required.
errors.byte={0} must be an byte.
errors.date={0} is not a date.
errors.double={0} must be an double.
errors.float={0} must be an float.
errors.integer={0} must be an integer.
errors.long={0} must be an long.
errors.short={0} must be an short.
errors.creditcard={0} is not a valid credit card number.
errors.email={0} is an invalid e-mail address.
# -- other --
errors.cancel=Operation cancelled.
errors.detail={0}
errors.general=The process did not complete. Details should follow.
errors.token=Request could not be completed. Operation is not in sequence.
# -- welcome --
welcome.title=Struts Application
welcome.heading=Struts Applications in Netbeans!
welcome.message=It's easy to create Struts applications with NetBeans.

UserAction.java


package Com.Candidjava;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class UserAction extends org.apache.struts.action.Action {

	/* forward name="success" path="" */
	private final static String SUCCESS = "success";

	/**
	 * This is the action called from the Struts framework.
	 *
	 * @param mapping
	 *            The ActionMapping used to select this instance.
	 * @param form
	 *            The optional ActionForm bean for this request.
	 * @param request
	 *            The HTTP Request we are processing.
	 * @param response
	 *            The HTTP Response we are processing.
	 * @throws java.lang.Exception
	 * @return
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		UserForm userForm = (UserForm) form;
		EmployeeData empData = new EmployeeData();
		userForm.setEmployeeList(empData.loadData());
		return mapping.findForward(SUCCESS);
	}
}

UserForm.java


package Com.Candidjava;

import java.util.ArrayList;

import org.apache.struts.action.ActionForm;

public class UserForm extends ActionForm {

	private ArrayList employeeList;

	public UserForm() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @return the employeeList
	 */
	public ArrayList getEmployeeList() {
		return employeeList;
	}

	/**
	 * @param employeeList
	 *            the employeeList to set
	 */
	public void setEmployeeList(ArrayList employeeList) {
		this.employeeList = employeeList;
	}

}

web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<servlet>
		<servlet-name>action</servlet-name>
		<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
		<init-param>
			<param-name>config</param-name>
			<param-value>/WEB-INF/struts-config.xml</param-value>
		</init-param>
		<init-param>
			<param-name>debug</param-name>
			<param-value>2</param-value>
		</init-param>
		<init-param>
			<param-name>detail</param-name>
			<param-value>2</param-value>
		</init-param>
		<load-on-startup>2</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>action</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
	<session-config>
		<session-timeout>30</session-timeout>
	</session-config>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	<jsp-config>
		<taglib>
			<taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
			<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
		</taglib>
		<taglib>
			<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
			<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
		</taglib>
		<taglib>
			<taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
			<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
		</taglib>
		<taglib>
			<taglib-uri>/WEB-INF/struts-nested.tld</taglib-uri>
			<taglib-location>/WEB-INF/struts-nested.tld</taglib-location>
		</taglib>
		<taglib>
			<taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri>
			<taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
		</taglib>
	</jsp-config>
</web-app>

struts-config.xml


<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">

<struts-config>
	<form-beans>
		<form-bean name="UserForm" type="Com.Candidjava.UserForm" />
	</form-beans>

	<global-exceptions>

	</global-exceptions>

	<global-forwards>
		<forward name="welcome" path="/Welcome.do" />
	</global-forwards>

	<action-mappings>
		<action input="/" name="UserForm" path="/userAction" scope="session"
			type="Com.Candidjava.UserAction">
			<forward name="success" path="/user.jsp" />
		</action>
		<action path="/Welcome" forward="/welcomeStruts.jsp" />
	</action-mappings>

	<controller processorClass="org.apache.struts.tiles.TilesRequestProcessor" />

	<message-resources parameter="Com/Candidjava/ApplicationResource" />

	<!--
		========================= Tiles plugin ===============================
	-->
	<!--
		This plugin initialize Tiles definition factory. This later can takes
		some parameters explained here after. The plugin first read parameters
		from web.xml, thenoverload them with parameters defined here. All
		parameters are optional. The plugin should be declared in each
		struts-config file. - definitions-config: (optional) Specify
		configuration file names. There can be several comma separated file
		names (default: ?? ) - moduleAware: (optional - struts1.1) Specify if
		the Tiles definition factory is module aware. If true (default), there
		will be one factory for each Struts module. If false, there will be
		one common factory for all module. In this later case, it is still
		needed to declare one plugin per module. The factory will be
		initialized with parameters found in the first initialized plugin
		(generally the one associated with the default module). true : One
		factory per module. (default) false : one single shared factory for
		all modules - definitions-parser-validate: (optional) Specify if xml
		parser should validate the Tiles configuration file. true : validate.
		DTD should be specified in file header (default) false : no validation

		Paths found in Tiles definitions are relative to the main context.
	-->
	<plug-in className="org.apache.struts.tiles.TilesPlugin">
		<set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" />
		<set-property property="moduleAware" value="true" />
	</plug-in>

	<!--
		============= Validator plugin	=============
	-->
	<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
		<set-property property="pathnames"
			value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" />
	</plug-in>

</struts-config>

 

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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<jsp:forward page="userAction.do" />
</body>
</html>

user.jsp


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="http://displaytag.sf.net" prefix="display"%>
<%@taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>User Details</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<display:table id="data" name="sessionScope.UserForm.employeeList"
	requestURI="/userAction.do" pagesize="10">
	<display:column property="id" title="Id" sortable="true" />
	<display:column property="userName" title="User Name" sortable="true" />
	<display:column property="emailId" title="Email Id" sortable="true" />
</display:table>
</body>
</html>

style.css


@charset "utf-8";

/* CSS Document */
table {
	border: 1px solid #666;
	width: 80%;
	margin: 20px 0 20px 0 !important;
}

th,td {
	padding: 2px 4px 2px 4px !important;
	text-align: left;
	vertical-align: top;
}

thead tr {
	background-color: #0B615E;
}

th.sorted {
	background-color: #0B615E;
}

th a,th a:visited {
	color: blue;
}

th a:hover {
	text-decoration: underline;
	color: black;
}

th.sorted a,th.sortable a {
	background-position: right;
	display: block;
	width: 100%;
}

th.sortable a {
	background-image: url(../img/arrow_off.png);
	background-repeat: no-repeat;
}

th.order1 a {
	background-image: url(../img/arrow_down.png);
	background-repeat: no-repeat;
}

th.order2 a {
	background-image: url(../img/arrow_up.png);
	background-repeat: no-repeat;
}

tr.odd {
	background-color: #fff
}

tr.tableRowEven,tr.even {
	background-color: #CCCCCC
}

.group-1 {
	font-weight: bold;
	padding-bottom: 10px;
	border-top: 1px solid black;
}

.group-2 {
	font-style: italic;
	border-top: 1px solid black;
}

.grouped-table tr.even {
	background-color: #fff;
}

.grouped-table tr.odd {
	background-color: #fff;
}

OUTPUT

DOWNLOAD+SOURCE CODE                                                                                                          BACK

Filed in: Z-AStruts Codes

No comments yet.

Leave a Reply