个性化阅读
专注于IT技术分析

Spring MVC分页示例图解

本文概述

分页用于在不同部分显示大量记录。在这种情况下, 我们将在一页中显示10、20或50条记录。对于其余记录, 我们提供链接。

我们可以简单地在Spring MVC中创建分页示例。在此分页示例中, 我们使用MySQL数据库来获取记录。


创建表或导入SQL文件

在这里, 我们在”测试”数据库中创建了” emp”表。 emp表具有三个字段:ID, 名称和薪水。创建一个表并手动插入记录或导入我们的SQL文件。


Spring MVC分页示例

1.将依赖项添加到pom.xml文件。

pom.xml

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.1.1.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper -->
<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-jasper</artifactId>
    <version>9.0.12</version>
</dependency>
    <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>  
    <groupId>javax.servlet</groupId>  
    <artifactId>servlet-api</artifactId>  
    <version>3.0-alpha-1</version>  
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>
    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.11</version>
</dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.1.1.RELEASE</version>
</dependency>

2.创建bean类

在这里, bean类包含对应于数据库中存在的字段的变量(以及setter和getter方法)。

Emp.java

package com.srcmini.beans;  
  
public class Emp {  
private int id;  
private String name;  
private float salary;  
  
public int getId() {  
    return id;  
}  
public void setId(int id) {  
    this.id = id;  
}  
public String getName() {  
    return name;  
}  
public void setName(String name) {  
    this.name = name;  
}  
public float getSalary() {  
    return salary;  
}  
public void setSalary(float salary) {  
    this.salary = salary;  
}  
  
}

3.创建控制器类

在Controller类中, @PathVariable批注将方法参数与临时URL绑定在一起。例如:

@RequestMapping(value="/viewemp/{pageid}")

在这里, {}括号包含临时值。

EmpController.java

package com.srcmini.controllers;   
import java.util.List;    
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;  
import org.springframework.web.bind.annotation.RequestMapping;    
import com.srcmini.beans.Emp;  
import com.srcmini.dao.EmpDao;  
@Controller  
public class EmpController {  
    @Autowired  
    EmpDao dao;  
      
    @RequestMapping(value="/viewemp/{pageid}")  
    public String edit(@PathVariable int pageid, Model m){  
        int total=5;  
        if(pageid==1){}  
        else{  
            pageid=(pageid-1)*total+1;  
        }  
        System.out.println(pageid);
        List<Emp> list=dao.getEmployeesByPage(pageid, total);  
          m.addAttribute("msg", list);
        return "viewemp";  
    }  
}

4.创建DAO类

让我们创建一个DAO类来访问数据库中所需的数据。

EmpDao.java

package com.srcmini.dao;  
 
import java.sql.ResultSet;  
import java.sql.SQLException;  
import java.util.List;  
import org.springframework.jdbc.core.JdbcTemplate;  
import org.springframework.jdbc.core.RowMapper;  
import com.srcmini.beans.Emp;  
  
public class EmpDao {  
JdbcTemplate template;  
  
public void setTemplate(JdbcTemplate template) {  
    this.template = template;  
}  
  
public List<Emp> getEmployeesByPage(int pageid, int total){  
    String sql="select * from emp limit "+(pageid-1)+", "+total;  
    return template.query(sql, new RowMapper<Emp>(){  
        public Emp mapRow(ResultSet rs, int row) throws SQLException {  
            Emp e=new Emp();  
            e.setId(rs.getInt(1));  
            e.setName(rs.getString(2));  
            e.setSalary(rs.getFloat(3));  
            return e;  
        }  
    });  
}  
}

5.在web.xml文件中提供控制器的条目

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">
  <display-name>SpringMVC</display-name>
   <servlet>  
    <servlet-name>spring</servlet-name>  
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    <load-on-startup>1</load-on-startup>    
</servlet>  
<servlet-mapping>  
    <servlet-name>spring</servlet-name>  
    <url-pattern>/</url-pattern>  
</servlet-mapping>  
</web-app>

6.在xml文件中定义bean

spring-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans
    	http://www.springframework.org/schema/beans/spring-beans.xsd
    	http://www.springframework.org/schema/context
    	http://www.springframework.org/schema/context/spring-context.xsd
    	http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.srcmini.controllers"></context:component-scan>  
  
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
<property name="prefix" value="/WEB-INF/jsp/"></property>  
<property name="suffix" value=".jsp"></property>  
</bean>  
  
<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>  
<property name="url" value="jdbc:mysql://localhost:3306/test"></property>  
<property name="username" value=""></property>  
<property name="password" value=""></property>  
</bean>  
  
<bean id="jt" class="org.springframework.jdbc.core.JdbcTemplate">  
<property name="dataSource" ref="ds"></property>  
</bean>  
  
<bean id="dao" class="com.srcmini.dao.EmpDao">  
<property name="template" ref="jt"></property>  
</bean>     
</beans>

7.创建请求的页面

index.jsp

<!DOCTYPE html>
<html>
<body>
<a href="viewemp/1">View Employees</a> 
</body>
</html>

8.创建视图组件

viewemp.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<h1>Employees List</h1>  
<table border="2" width="70%" cellpadding="2">  
<tr><th>Id</th><th>Name</th><th>Salary</th></tr>  
   <c:forEach var="emp" items="${msg}">   
   <tr>  
   <td>${emp.id}</td>  
   <td>${emp.name}</td>  
   <td>${emp.salary}</td>  
   </tr>  
   </c:forEach>  
   </table>  
   <br/>  
   <a href="/SpringMVCPaginationExample/viewemp/1">1</a>   
   <a href="/SpringMVCPaginationExample/viewemp/2">2</a>   
   <a href="/SpringMVCPaginationExample/viewemp/3">3</a>  
</body>
</html>

输出

Spring MVC分页示例
Spring MVC分页示例
Spring MVC分页示例
Spring MVC分页示例

下载此示例(使用Eclipse开发)


下载SQL文件

下载SQL文件

下载MYSQL-connector.jar文件

如果你不使用maven, 请下载MYSQL-connector.jar。

赞(0)
未经允许不得转载:srcmini » Spring MVC分页示例图解

评论 抢沙发

评论前必须登录!