Monday, 4 January 2016

Implementing Pagination with Spring JDBC



















JSP Code :

     <span style="float: right;margin-right: 150px;">
      ${(fruitPaginationForm.currentPage-1)*fruitPaginationForm.recordsPerPage+1}

         -
       <c:if test="${(fruitPaginationForm.currentPage-1)*fruitPaginationForm.recordsPerPage+fruitPaginationForm.recordsPerPage gt fruitPaginationForm.noOfRecords}">
          ${fruitPaginationForm.noOfRecords}
       </c:if>
       
       <c:if test="${(fruitPaginationForm.currentPage-1)*fruitPaginationForm.recordsPerPage+fruitPaginationForm.recordsPerPage lt fruitPaginationForm.noOfRecords}">
       ${(fruitPaginationForm.currentPage-1)*fruitPaginationForm.recordsPerPage+fruitPaginationForm.recordsPerPage}
       </c:if>

       of  ${fruitPaginationForm.noOfRecords}  
     </span>


Code for Next and Previous Button on JSP

<c:if test="${fruitPaginationForm.currentPage != 1}">
            <td><a href="${pageContext.request.contextPath}/fruitWithPagination.do?page=${fruitPaginationForm.currentPage - 1}">Previous</a></td>
        </c:if>
        &nbsp;&nbsp; | &nbsp;&nbsp;
        <c:if test="${fruitPaginationForm.currentPage lt fruitPaginationForm.noOfPages}">
            <td><a href="${pageContext.request.contextPath}/fruitWithPagination.do?page=${fruitPaginationForm.currentPage + 1}">Next</a></td>
        </c:if>


Controller Code

@RequestMapping(value="fruitWithPagination.do",method=RequestMethod.GET)
public String fruitWithPagination(@RequestParam(value="page",required=false)String  page,Model model) {
int recordsPerPage=3;
int currentPage=0;
if(page==null) {
currentPage=1;
}else{
currentPage=Integer.parseInt(page);
}
FruitPaginationForm fruitPaginationForm = fruitService.findFruitsWithPagination( (currentPage-1)*recordsPerPage, recordsPerPage);
fruitPaginationForm.setCurrentPage(currentPage);
fruitPaginationForm.initPagination(); //initializing number of total pages......
model.addAttribute("fruitPaginationForm", fruitPaginationForm);
return "tfruits"; //give view name with out extension........../fruitSearch.jsp

}


public class FruitPaginationForm {

private int currentPage;
private int noOfRecords;
private List<FruitForm> fruitFormList;
private int recordsPerPage=3;
private int noOfPages;

public void initPagination(){
noOfPages=(int) Math.ceil(noOfRecords * 1.0 / recordsPerPage);

}
}



Service Layer Code

@Override
public FruitPaginationForm findFruitsWithPagination(int start,
int noOfRecords) {

FruitPaginationEntity fruitPaginationEntity=fruitDao.findFruitsWithPagination(start, noOfRecords);
FruitPaginationForm fruitPaginationForm=new FruitPaginationForm();
BeanUtils.copyProperties(fruitPaginationEntity, fruitPaginationForm);

List<FruitForm> fruitFormList=new ArrayList<FruitForm>();
List<FruitEntity> fruitEntities=fruitPaginationEntity.getFruitEntityList();
for(FruitEntity fe:fruitEntities){
  FruitForm form=new FruitForm();
 BeanUtils.copyProperties(fe, form);
 fruitFormList.add(form);
}
fruitPaginationForm.setFruitFormList(fruitFormList);
return fruitPaginationForm;

}


Dao Layer Code with MySQL Database

@Override

public FruitPaginationEntity findFruitsWithPagination(int start, int noOfRecords) {

        String query = "select  * from fruit_tbl limit " + start + ", " + noOfRecords;
  
         List<FruitEntity> fruitEntityList = (List<FruitEntity>) jdbcTemplate  .query(query,
                        new BeanPropertyRowMapper(FruitEntity.class));
      
        // To obtain this row count, include a SQL_CALC_FOUND_ROWS option in the
        // SELECT statement, and then invoke FOUND_ROWS() afterward
   
         FruitPaginationEntity fruitPaginationEntity = new FruitPaginationEntity();
        
        int tnoOfRecords = jdbcTemplate.queryForInt("select count(*)  from fruit_tbl");
      
         fruitPaginationEntity.setNoOfRecords(tnoOfRecords);
        fruitPaginationEntity.setFruitEntityList(fruitEntityList);
        /* query a total number of rows from database. */
        return fruitPaginationEntity;




 */
public class FruitPaginationEntity {

private int noOfRecords;
private List<FruitEntity> fruitEntityList;

public int getNoOfRecords() {
return noOfRecords;

}
}


No comments:

Post a Comment