Sunday, September 15, 2019

Spring MVC Pagination Example

Pagination is used to display a 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.

Create a 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 a table and insert records manually or import our SQL file.


Spring MVC Pagination Example

1. Add dependencies to pom.xml file.

pom.xml
  1.  <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->  
  2. <dependency>  
  3.     <groupId>org.springframework</groupId>  
  4.     <artifactId>spring-webmvc</artifactId>  
  5.     <version>5.1.1.RELEASE</version>  
  6. </dependency>  
  7.   
  8. <!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper -->  
  9. <dependency>  
  10.     <groupId>org.apache.tomcat</groupId>  
  11.     <artifactId>tomcat-jasper</artifactId>  
  12.     <version>9.0.12</version>  
  13. </dependency>  
  14.     <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->  
  15. <dependency>    
  16.     <groupId>javax.servlet</groupId>    
  17.     <artifactId>servlet-api</artifactId>    
  18.     <version>3.0-alpha-1</version>    
  19. </dependency>  
  20. <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->  
  21. <dependency>  
  22.     <groupId>javax.servlet</groupId>  
  23.     <artifactId>jstl</artifactId>  
  24.     <version>1.2</version>  
  25. </dependency>  
  26.     <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->  
  27. <dependency>  
  28.     <groupId>mysql</groupId>  
  29.     <artifactId>mysql-connector-java</artifactId>  
  30.     <version>8.0.11</version>  
  31. </dependency>  
  32.     <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->  
  33. <dependency>  
  34.     <groupId>org.springframework</groupId>  
  35.     <artifactId>spring-jdbc</artifactId>  
  36.     <version>5.1.1.RELEASE</version>  
  37. </dependency>  

2. Create the bean class

Here, the bean class contains the variables (along setter and getter methods) corresponding to the fields exist in the database.
Emp.java
  1. package com.javatpoint.beans;    
  2.     
  3. public class Emp {    
  4. private int id;    
  5. private String name;    
  6. private float salary;    
  7.     
  8. public int getId() {    
  9.     return id;    
  10. }    
  11. public void setId(int id) {    
  12.     this.id = id;    
  13. }    
  14. public String getName() {    
  15.     return name;    
  16. }    
  17. public void setName(String name) {    
  18.     this.name = name;    
  19. }    
  20. public float getSalary() {    
  21.     return salary;    
  22. }    
  23. public void setSalary(float salary) {    
  24.     this.salary = salary;    
  25. }    
  26.     
  27. }  

3. Create the controller class

In Controller class, the @PathVariable annotation bounds the method parameter with a temporary URL. For example:
  1. @RequestMapping(value="/viewemp/{pageid}")    
Here, {} bracket contains the temporary value.
EmpController.java
  1. package com.javatpoint.controllers;     
  2. import java.util.List;      
  3. import org.springframework.beans.factory.annotation.Autowired;    
  4. import org.springframework.stereotype.Controller;  
  5. import org.springframework.ui.Model;  
  6. import org.springframework.web.bind.annotation.PathVariable;    
  7. import org.springframework.web.bind.annotation.RequestMapping;      
  8. import com.javatpoint.beans.Emp;    
  9. import com.javatpoint.dao.EmpDao;    
  10. @Controller    
  11. public class EmpController {    
  12.     @Autowired    
  13.     EmpDao dao;    
  14.         
  15.     @RequestMapping(value="/viewemp/{pageid}")    
  16.     public String edit(@PathVariable int pageid,Model m){    
  17.         int total=5;    
  18.         if(pageid==1){}    
  19.         else{    
  20.             pageid=(pageid-1)*total+1;    
  21.         }    
  22.         System.out.println(pageid);  
  23.         List<Emp> list=dao.getEmployeesByPage(pageid,total);    
  24.           m.addAttribute("msg", list);  
  25.         return "viewemp";    
  26.     }    
  27. }   

4. Create the DAO class

Let's create a DAO class to access the required data from the database.
EmpDao.java
  1. package com.javatpoint.dao;    
  2.    
  3. import java.sql.ResultSet;    
  4. import java.sql.SQLException;    
  5. import java.util.List;    
  6. import org.springframework.jdbc.core.JdbcTemplate;    
  7. import org.springframework.jdbc.core.RowMapper;    
  8. import com.javatpoint.beans.Emp;    
  9.     
  10. public class EmpDao {    
  11. JdbcTemplate template;    
  12.     
  13. public void setTemplate(JdbcTemplate template) {    
  14.     this.template = template;    
  15. }    
  16.     
  17. public List<Emp> getEmployeesByPage(int pageid,int total){    
  18.     String sql="select * from emp limit "+(pageid-1)+","+total;    
  19.     return template.query(sql,new RowMapper<Emp>(){    
  20.         public Emp mapRow(ResultSet rs, int row) throws SQLException {    
  21.             Emp e=new Emp();    
  22.             e.setId(rs.getInt(1));    
  23.             e.setName(rs.getString(2));    
  24.             e.setSalary(rs.getFloat(3));    
  25.             return e;    
  26.         }    
  27.     });    
  28. }    
  29. }    

5. Provide the entry of controller in the web.xml file

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

6. Define the bean in the xml file

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:context="http://www.springframework.org/schema/context"  
  5.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  6.     xsi:schemaLocation="  
  7.         http://www.springframework.org/schema/beans  
  8.         http://www.springframework.org/schema/beans/spring-beans.xsd  
  9.         http://www.springframework.org/schema/context  
  10.         http://www.springframework.org/schema/context/spring-context.xsd  
  11.         http://www.springframework.org/schema/mvc  
  12.         http://www.springframework.org/schema/mvc/spring-mvc.xsd">  
  13. <context:component-scan base-package="com.javatpoint.controllers"></context:component-scan>    
  14.     
  15. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">    
  16. <property name="prefix" value="/WEB-INF/jsp/"></property>    
  17. <property name="suffix" value=".jsp"></property>    
  18. </bean>    
  19.     
  20. <bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">    
  21. <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>    
  22. <property name="url" value="jdbc:mysql://localhost:3306/test"></property>    
  23. <property name="username" value=""></property>    
  24. <property name="password" value=""></property>    
  25. </bean>    
  26.     
  27. <bean id="jt" class="org.springframework.jdbc.core.JdbcTemplate">    
  28. <property name="dataSource" ref="ds"></property>    
  29. </bean>    
  30.     
  31. <bean id="dao" class="com.javatpoint.dao.EmpDao">    
  32. <property name="template" ref="jt"></property>    
  33. </bean>       
  34. </beans>  

7. Create the requested page

index.jsp
  1. <!DOCTYPE html>  
  2. <html>  
  3. <body>  
  4. <a href="viewemp/1">View Employees</a>   
  5. </body>  
  6. </html>  

8. Create view component

viewemp.jsp
  1. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>      
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  3. <html>  
  4. <body>  
  5. <h1>Employees List</h1>    
  6. <table border="2" width="70%" cellpadding="2">    
  7. <tr><th>Id</th><th>Name</th><th>Salary</th></tr>    
  8.    <c:forEach var="emp" items="${msg}">     
  9.    <tr>    
  10.    <td>${emp.id}</td>    
  11.    <td>${emp.name}</td>    
  12.    <td>${emp.salary}</td>    
  13.    </tr>    
  14.    </c:forEach>    
  15.    </table>    
  16.    <br/>    
  17.    <a href="/SpringMVCPaginationExample/viewemp/1">1</a>     
  18.    <a href="/SpringMVCPaginationExample/viewemp/2">2</a>     
  19.    <a href="/SpringMVCPaginationExample/viewemp/3">3</a>    
  20. </body>  
  21. </html>  
Output:
Spring MVC Pagination Example
Spring MVC Pagination Example
Spring MVC Pagination Example
Spring MVC Pagination Example

No comments:

Post a Comment

How to DROP SEQUENCE in Oracle?

  Oracle  DROP SEQUENCE   overview The  DROP SEQUENCE  the statement allows you to remove a sequence from the database. Here is the basic sy...