Controller class -
List<UserInfo> loadUserInfoDetails = service.getAllUserInfoDetails();
if(loadUserInfoDetails.size() > CommonUtils.PAGE_MAX_VALUE) {
int pageNumberRequest = 0;
if( !request.getParameter("page").isEmpty() ) {
pageNumberRequest = Integer.parseInt(request.getParameter("page"));
}
commonPaginationService.makePaginationLogic(CommonUtils.PAGE_MIN_VALUE, CommonUtils.PAGE_MAX_VALUE, loadUserInfoDetails, mav, pageNumberRequest);
}
mav.setViewName("admin/welcome");
CommonPagination Class -
package com.techa2zsoln.allcustom.services;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;
@Service
public class CommonPaginationService {
/**
* @param pageMinValue
* @param pageMaxValue
* @param loadUserInfoDetails
* @param mav
* @param pageNumberRequest
*/
public void makePaginationLogic(int pageMinValue, int pageMaxValue, List<?> loadUserInfoDetails, ModelAndView mav, int pageNumberRequest) {
List<?> loadUserInfoFilterData;
List<Integer> listSequence = new ArrayList<Integer>();
/**
* to define number of page should be form.
* */
if( ( (loadUserInfoDetails.size() ) %pageMaxValue) == 0 ) {
mav.addObject("pageList", ( loadUserInfoDetails.size() /pageMaxValue)-1 );
}else {
mav.addObject("pageList", loadUserInfoDetails.size()/pageMaxValue);
}
if( ( loadUserInfoDetails.size() %pageMaxValue) != 0 ) {
for(int i=1 ; i < ( loadUserInfoDetails.size() /pageMaxValue) ; i++) {
listSequence.add(i);
}
mav.addObject("listSequence", listSequence);
}
// After manage inputs display total records.
if(pageNumberRequest == 0) {
loadUserInfoFilterData = loadUserInfoDetails.subList(pageMinValue, pageMaxValue);
mav.addObject("userInfoDetails", loadUserInfoFilterData);
}else {
int diffrence = pageMaxValue - pageMinValue;
pageMinValue = (diffrence * pageNumberRequest);
pageMaxValue = pageMinValue + diffrence;
if ( (pageMaxValue) > (loadUserInfoDetails.size()) ) {
loadUserInfoFilterData = loadUserInfoDetails.subList(pageMinValue, loadUserInfoDetails.size());
}else {
loadUserInfoFilterData = loadUserInfoDetails.subList(pageMinValue, pageMaxValue);
}
mav.addObject("userInfoDetails", loadUserInfoFilterData);
}
// total size of records.
mav.addObject("totalRecordSize", loadUserInfoDetails);
}
}
List<UserInfo> loadUserInfoDetails = service.getAllUserInfoDetails();
if(loadUserInfoDetails.size() > CommonUtils.PAGE_MAX_VALUE) {
int pageNumberRequest = 0;
if( !request.getParameter("page").isEmpty() ) {
pageNumberRequest = Integer.parseInt(request.getParameter("page"));
}
commonPaginationService.makePaginationLogic(CommonUtils.PAGE_MIN_VALUE, CommonUtils.PAGE_MAX_VALUE, loadUserInfoDetails, mav, pageNumberRequest);
}
mav.setViewName("admin/welcome");
CommonPagination Class -
package com.techa2zsoln.allcustom.services;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;
@Service
public class CommonPaginationService {
/**
* @param pageMinValue
* @param pageMaxValue
* @param loadUserInfoDetails
* @param mav
* @param pageNumberRequest
*/
public void makePaginationLogic(int pageMinValue, int pageMaxValue, List<?> loadUserInfoDetails, ModelAndView mav, int pageNumberRequest) {
List<?> loadUserInfoFilterData;
List<Integer> listSequence = new ArrayList<Integer>();
/**
* to define number of page should be form.
* */
if( ( (loadUserInfoDetails.size() ) %pageMaxValue) == 0 ) {
mav.addObject("pageList", ( loadUserInfoDetails.size() /pageMaxValue)-1 );
}else {
mav.addObject("pageList", loadUserInfoDetails.size()/pageMaxValue);
}
if( ( loadUserInfoDetails.size() %pageMaxValue) != 0 ) {
for(int i=1 ; i < ( loadUserInfoDetails.size() /pageMaxValue) ; i++) {
listSequence.add(i);
}
mav.addObject("listSequence", listSequence);
}
// After manage inputs display total records.
if(pageNumberRequest == 0) {
loadUserInfoFilterData = loadUserInfoDetails.subList(pageMinValue, pageMaxValue);
mav.addObject("userInfoDetails", loadUserInfoFilterData);
}else {
int diffrence = pageMaxValue - pageMinValue;
pageMinValue = (diffrence * pageNumberRequest);
pageMaxValue = pageMinValue + diffrence;
if ( (pageMaxValue) > (loadUserInfoDetails.size()) ) {
loadUserInfoFilterData = loadUserInfoDetails.subList(pageMinValue, loadUserInfoDetails.size());
}else {
loadUserInfoFilterData = loadUserInfoDetails.subList(pageMinValue, pageMaxValue);
}
mav.addObject("userInfoDetails", loadUserInfoFilterData);
}
// total size of records.
mav.addObject("totalRecordSize", loadUserInfoDetails);
}
}
Jsp Page -
<!-- Start Pagination -->
<c:if test="${pageList eq 1}">
<a href="?page=1" >Start</a>
</c:if>
<c:if test="${pageList gt 1}">
<a href="?page=0" >Start</a>
<a href=""><<</a>
<c:set value="1" var="pagenumber" />
<c:forEach items="${listSequence}" var="pagination">
<c:if test="${pagination >= pagenumber}">
<a href="?page=${pagenumber}" >${pagenumber}</a>
<c:set var="pagenumber" value="${pagenumber + 1}" />
</c:if>
</c:forEach>
<a href="">>></a>
<a href="?page=<c:out value="${pageList}" />" >End</a>
</c:if>
<!-- End Pagination -->