This blogs tell how to do pagination using Struts 2.0, Spring 3.0 and Hibernate 3.0 step by step. This blog is extension of http://tiwarij2eeblog.blogspot.com/2011/09/storing-and-retrieving-images-in-db.html.
Here we will create search page which will display results with pagination.
Step 1: Setup eclipse as per above link and add some data to DB.
Step 2: Create anew Java bean as below
public class UserSearchBean {
private int selectedPageNumber;
private int totalPages;
private int serialNumberAddfactor;
private int noOfRecordsPerPage = 3; // Default
private String from;
// Search creteria
private String firstName;
private String lastName;
private List<User> selectedUserList;
// Write Setters and getters
Step 3: Create a new java Action class as below
package org.paandav.blog.action;
import org.paandav.bolg.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class SearchUserAction extends ActionSupport implements
ModelDriven<UserSearchBean> {
private static final long serialVersionUID = 1L;
private UserSearchBean model = new UserSearchBean();
private UserService userService;
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
public void setModel(UserSearchBean model) {
this.model = model;
}
@Override
public String execute() throws Exception {
if (!"INIT".equals(getModel().getFrom())) {
userService.getPaginatedResultForUserSearch(getModel());
}
return INPUT;
}
@Override
public UserSearchBean getModel() {
return model;
}
}
Step 4: Create new method getPaginatedResultForUserSearch in UserService.java and write implementation as below
UserService.java
UserSearchBean getPaginatedResultForUserSearch(UserSearchBean usb)
throws Exception;
UserServiceImpl.java
@Override
public UserSearchBean getPaginatedResultForUserSearch(UserSearchBean usb)
throws Exception {
return userDao.getPaginatedResultForUserSearch(usb);
}
Step 5: Create new method UserDAO and implement it as belwo
UserDAO.java
UserSearchBean getPaginatedResultForUserSearch(UserSearchBean usb)
throws Exception;
UserDAOImpl.java
@Override
public UserSearchBean getPaginatedResultForUserSearch(UserSearchBean usb)
throws Exception {
List<User> selectedUser = null;
Criteria crit = getCreteria(usb);
int totalNoOfRecords = (Integer) crit.setProjection(
Projections.rowCount()).uniqueResult();
int startIndex = 0;
int totalPages = 1;
if (totalNoOfRecords > usb.getNoOfRecordsPerPage()) {
double noOfPages = (double) totalNoOfRecords
/ (double) usb.getNoOfRecordsPerPage();
totalPages = (int) noOfPages;
if (noOfPages % totalPages > 0.0) {
totalPages++;
}
}
if (usb.getSelectedPageNumber() > 1) {
startIndex = usb.getNoOfRecordsPerPage()
* (usb.getSelectedPageNumber() - 1);
usb.setSerialNumberAddfactor(usb.getNoOfRecordsPerPage()
* (usb.getSelectedPageNumber() - 1));
}
usb.setTotalPages(totalPages);
crit = getCreteria(usb);
crit.setFirstResult(startIndex);
crit.setMaxResults(usb.getNoOfRecordsPerPage());
selectedUser = crit.list();
usb.setSelectedUserList(selectedUser);
return usb;
}
private Criteria getCreteria(UserSearchBean usb) {
Criteria criteria = getSession().createCriteria(User.class);
if (usb.getFirstName() != null && !"".equals(usb.getFirstName())) {
criteria.add(Restrictions.ilike("firstName", usb.getFirstName()));
}
if (usb.getLastName() != null && !"".equals(usb.getLastName())) {
criteria.add(Restrictions.ilike("lastName", usb.getLastName()));
}
return criteria;
}
Step 6: Now create a new actiom mapping in struts.xml as below
<action name="searchUser" class="org.paandav.blog.action.SearchUserAction">
<result name="input">
WEB-INF/jsp/userSearchPage.jsp
</result>
</action>
Step 7: Now create anew jsp file userSearchPage.jsp as below
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body style="font-family: verdana;">
<s:form action="searchUser" theme="simple">
<table align="center" border=1>
<tr bgcolor="aqua">
<td>First Name <s:textfield name="firstName"></s:textfield></td>
<td>Last Name <s:textfield name="lastName"></s:textfield></td>
<td colspan="3"><s:submit value="Search User"></s:submit></td>
</tr>
<s:if test="totalPages > 1">
<tr bgcolor="lightgrey">
<td colspan="5">Page No :<s:iterator value="totalPages.{#this}"
status="stat">
<s:submit value="%{#stat.count}" name="selectedPageNumber"></s:submit>
</s:iterator></td>
</tr>
</s:if>
<s:if test="selectedUserList != null && selectedUserList.size > 0">
<tr bgcolor="aqua">
<th>S.N</th>
<th>First Name</th>
<th>Last Name</th>
<th>Content type</th>
<th>File Name</th>
</tr>
<s:iterator value="selectedUserList" status="stat">
<tr bgcolor="#FFF288">
<td><s:property value="#stat.count + serialNumberAddfactor" /></td>
<td><s:property value="firstName" /></td>
<td><s:property value="lastName" /></td>
<td><s:property value="conTentType" /></td>
<td><s:property value="fileName" /></td>
</tr>
</s:iterator>
</s:if>
</table>
</s:form>
</body>
</html>
Step 9: Now start the server and access the URL
You will see a screen like below
Now if you don't fill any data in boxes it will display all data in table with pagination like belwo
Now you can try with filling some criteria and search.