Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

MVC Pagination Example


Pagination is used to display large number of records in different parts. In such case, we display 10, 20 or 50 records in one page. For remaining records, we provide links.

We can simply create pagination example in Spring MVC. In this pagination example, we are using MySQL database to fetch records.


Required Jar files

To run this example, you need to load:

  • Spring Core jar files
  • Spring Web jar files
  • mysql-connector.jar file

download all the jar files for spring including core, web, aop, mvc, j2ee, remoting, oxm, jdbc, orm etc.


Create table or import sql file

Here, we have created “emp” table in “test” database. The emp table has three fields: id, name and salary. Either create table and insert records manually or import our sql file.


Spring MVC Pagination Example

index.jsp

  1. <a href=“viewemp/1”>View Employees</a>

Emp.java

  1. package com.Kreationnext.beans;
  2. public class Emp {
  3. private int id;
  4. private String name;
  5. private float salary;
  6. public int getId() {
  7.     return id;
  8. }
  9. public void setId(int id) {
  10.     this.id = id;
  11. }
  12. public String getName() {
  13.     return name;
  14. }
  15. public void setName(String name) {
  16.     this.name = name;
  17. }
  18. public float getSalary() {
  19.     return salary;
  20. }
  21. public void setSalary(float salary) {
  22.     this.salary = salary;
  23. }
  24. }

EmpDao.java

  1. package com.Kreationnext.dao;
  2. import java.sql.ResultSet;
  3. import java.sql.SQLException;
  4. import java.util.List;
  5. import org.springframework.jdbc.core.JdbcTemplate;
  6. import org.springframework.jdbc.core.RowMapper;
  7. import com.Kreationnext.beans.Emp;
  8. public class EmpDao {
  9. JdbcTemplate template;
  10. public void setTemplate(JdbcTemplate template) {
  11.     this.template = template;
  12. }
  13. public List<Emp> getEmployeesByPage(int pageid,int total){
  14.     String sql=“select * from Emp limit “+(pageid-1)+“,”+total;
  15.     return template.query(sql,new RowMapper<Emp>(){
  16.         public Emp mapRow(ResultSet rs, int row) throws SQLException {
  17.             Emp e=new Emp();
  18.             e.setId(rs.getInt(1));
  19.             e.setName(rs.getString(2));
  20.             e.setSalary(rs.getFloat(3));
  21.             return e;
  22.         }
  23.     });
  24. }
  25. }

EmpController.java

  1. package com.Kreationnext.controllers;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.web.bind.annotation.ModelAttribute;
  7. import org.springframework.web.bind.annotation.PathVariable;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.RequestMethod;
  10. import org.springframework.web.servlet.ModelAndView;
  11. import com.Kreationnext.beans.Emp;
  12. import com.Kreationnext.dao.EmpDao;
  13. @Controller
  14. public class EmpController {
  15.     @Autowired
  16.     EmpDao dao;
  17.     @RequestMapping(value=“/viewemp/{pageid}”)
  18.     public ModelAndView edit(@PathVariable int pageid){
  19.         int total=5;
  20.         if(pageid==1){}
  21.         else{
  22.             pageid=(pageid-1)*total+1;
  23.         }
  24.         List<Emp> list=dao.getEmployeesByPage(pageid,total);
  25.         return new ModelAndView(“viewemp”,“list”,list);
  26.     }
  27. }

web.xml

  1. <?xml version=“1.0” encoding=“UTF-8”?>
  2. <web-app version=“2.5”
  3.     xmlns=“http://java.sun.com/xml/ns/javaee”
  4.     xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
  5.     xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee 
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd”>
  7.  <servlet>
  8.     <servlet-name>spring</servlet-name>
  9.     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  10.     <load-on-startup>1</load-on-startup>
  11. </servlet>
  12. <servlet-mapping>
  13.     <servlet-name>spring</servlet-name>
  14.     <url-pattern>/</url-pattern>
  15. </servlet-mapping>
  16. </web-app>

spring-servlet.xml

  1. <?xml version=“1.0” encoding=“UTF-8”?>
  2. <beans xmlns=“http://www.springframework.org/schema/beans”
  3.     xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
  4.     xmlns:p=“http://www.springframework.org/schema/p”
  5.     xmlns:context=“http://www.springframework.org/schema/context”
  6.     xsi:schemaLocation=”http://www.springframework.org/schema/beans  
  7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  8. http://www.springframework.org/schema/context  
  9. http://www.springframework.org/schema/context/spring-context-3.0.xsd”>  
  10. <context:component-scan base-package=“com.Kreationnext.controllers”></context:component-scan>
  11. <bean class=“org.springframework.web.servlet.view.InternalResourceViewResolver”>
  12. <property name=“prefix” value=“/WEB-INF/jsp/”></property>
  13. <property name=“suffix” value=“.jsp”></property>
  14. </bean>
  15. <bean id=“ds” class=“org.springframework.jdbc.datasource.DriverManagerDataSource”>
  16. <property name=“driverClassName” value=“com.mysql.jdbc.Driver”></property>
  17. <property name=“url” value=“jdbc:mysql://localhost:3306/test”></property>
  18. <property name=“username” value=“”></property>
  19. <property name=“password” value=“”></property>
  20. </bean>
  21. <bean id=“jt” class=“org.springframework.jdbc.core.JdbcTemplate”>
  22. <property name=“dataSource” ref=“ds”></property>
  23. </bean>
  24. <bean id=“dao” class=“com.Kreationnext.dao.EmpDao”>
  25. <property name=“template” ref=“jt”></property>
  26. </bean>
  27. </beans>

viewemp.jsp

  1.     <%@ taglib uri=“http://java.sun.com/jsp/jstl/core” prefix=“c”%>
  2. <h1>Employees List</h1>
  3. <table border=“2” width=“70%” cellpadding=“2”>
  4. <tr><th>Id</th><th>Name</th><th>Salary</th></tr>
  5.    <c:forEach var=“emp” items=“${list}”>
  6.    <tr>
  7.    <td>${emp.id}</td>
  8.    <td>${emp.name}</td>
  9.    <td>${emp.salary}</td>
  10.    </tr>
  11.    </c:forEach>
  12.    </table>
  13.    <br/>
  14.    <a href=“/SpringMVC/viewemp/1”>1</a>
  15.    <a href=“/SpringMVC/viewemp/2”>2</a>
  16.    <a href=“/SpringMVC/viewemp/3”>3</a>

Output

spring-mvc-pagination-output1

spring-mvc-pagination-output2

spring-mvc-pagination-output3

spring-mvc-pagination-output4

Download SQL File

Download SQL File

Download mysql-connector.jar file

Download mysql-connector.jar

Download Spring MVC Pagination Example

We have created this application in MyEclipse IDE which already provides the jar files. If you use eclipse or other IDE’s, you need to load the jar file for spring MVC.

 

Comments

comments