项目-----学生管理系统04------springMVC测试和分页

本文介绍如何在SpringMVC项目中进行测试和实现分页效果。重点讲解了逻辑分页的高效实现,从Controller到Service再到DAO的流程,并提供了测试类的编写方法。同时展示了在show.jsp中使用Bootstrap展示分页结果的代码片段。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言:今天要掌握两个知识点。之前我们了解过spring的测试类,今天来了解springMVC的测试类。当我们在百度搜索时它只会出现几条数据,如果还想查看就要点击下一页。这就是分页效果。

分页:

   1、物理分页:查询出所有的数据,每页显示N条数据

   2、逻辑分页:每次查询N条数据,显示N条数据

从上面来看,逻辑分页效率跟高所以我们使用逻辑分页

还是用上次的顺序来:controller---->Service--------->dao

上面的流程不了解的点击链接:https://mp.youkuaiyun.com/postedit/85252423

一、controller

package com.controller;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.bean.Sbadbehavior;
import com.bean.Sbasicmessage;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.servlet.BasicService;
@Controller
public class BasicMsgController {
	@Autowired
	BasicService basicService;
	/**
	 * 学生基本信息
	 *
	@RequestMapping("allSta")//如果没有传值defaultValue默认的值
	public String getAllStaInfo(@RequestParam(value="pn",defaultValue="1")Integer pn,Model model){
		PageHelper.startPage(pn, 10);//使用myBatis分页查询。第一个值:相当于当前页,
		List<Sbasicmessage> bas= basicService.getallStu();
		PageInfo page=new PageInfo(bas,3);//使用pageinfo封装查询结果
		model.addAttribute("pageInfo", page);
		return "show";
	}
}

注意:Ctrl+shift+T:输入pageInFo(查看pageHelper的源代码,里面有很多属性都有中文可以了解一下)

二、Service

package com.servlet;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bean.Sbadbehavior;
import com.bean.Sbasicmessage;
import com.dao.SbadbehaviorMapper;
import com.dao.SbasicmessageMapper;
@Service
public class BasicService {
	@Autowired
	SbasicmessageMapper sbasicmessageMapper;//接口

	public List<Sbasicmessage> getallStu() {
        //用接口调用select方法,应映射文件中标签的id与select方法同名,所以才可以查询到数据库。将查询的数据给list集合名字list
		List<Sbasicmessage> list= sbasicmessageMapper.selectByExample(null);
		return list;
	}
}

三、TestMVC(后台逻辑已经写好了,要写一个测试类模拟发送请求)

package com.test;

import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import com.bean.Sbadbehavior;
import com.github.pagehelper.PageInfo;

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration//通过容器,容器工厂申请这次测试通过
@ContextConfiguration(locations = { "classpath:spring.xml",
		"file:src/main/webapp/WEB-INF/springMVC-servlet.xml" })
public class TestMVC {
	//自动注入一个容器
	@Autowired
	WebApplicationContext context;
	// 虚拟的mvc请求
	MockMvc mockmvc;

	/**
	 * 初始化mockmvc
	 */
	@Before//在---之前
	public void initMockMvc() {
		mockmvc = MockMvcBuilders.webAppContextSetup(context).build();
	}

	/**
	 * 测试分页方法
	 * 
	 * @throws Exception
	 */
	@Test//测试,所有的测试类都有
	public void testpage() throws Exception {
		//模拟请求,并拿到返回值
		MvcResult result = mockmvc.perform(MockMvcRequestBuilders.get("/allSta").param("pn", "1"))
				.andReturn();//返回测试结果
		//用MvcResult对象调用getRequest方法,得到MockHTTPServletRequest对象
		MockHttpServletRequest  request = result.getRequest();
		//用MockHTTPServletRequest对象调用getAttribute方法,得到PageInfo对象,就可以通过对象获取总计入数等等
		PageInfo info = (PageInfo) request.getAttribute("pageInfo");
		System.out.println(""+info.getPages());
		System.out.println("一共有"+info.getTotal()+"条数据");
		//获取所有学生
		List<Sbadbehavior> studentlist = info.getList();
		for(Sbadbehavior b:studentlist){
			System.out.println(b.getSbId());
		}
	}
}

四、show.jsp(实现分页效果)

上面是用的样式是使用的bootstrap,使用bootstrap的方法点击:https://mp.youkuaiyun.com/postedit/85125032

这就是主要的代码

<c:forEach items="${pageInfo.list}" var="stu">
                            <tr>
                                <td>${stu.sBid}</td>
                                <td>${stu.sName}</td>
                                <td>${stu.sSex==1?'男':'女'}</td>
                                <td>${stu.sAddr}</td>
                                <td>${stu.sPhone}</td>
                            </tr>
                        
      </c:forEach>

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<%
	pageContext.setAttribute("APP_PATH", request.getContextPath());
%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link
	href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css"
	rel="stylesheet">
<title>学生信息展示页</title>
</head>
<body>
	<div class="container-fluid">
		<div class="row">
			<!-- 第一层布局, -->
			
			<nav class="navbar navbar-default navbar-static-top">
			<div class="container">
			<div class="col-md-4 col-md-offset-4">
				<ul class="nav nav-pills">
					
						<li role="presentation" class="active"><a href="#">学生基本信息</a></li>
				
						<li role="presentation"><a href="#">缺勤</a></li>
					
						<li role="presentation"><a href="#">犯忌</a></li>
				
				</ul>
					</div>
			</div>
			</nav>
		


		</div>
		<div class="row">
			<!-- 第二层布局, -->
			<div class="col-md-2 col-md-offset-6">
				<form class="form-inline">
					<div class="form-group">
						<input type="text" class="form-control" id="exampleInputName2"
							placeholder="Jane Doe">
					</div>
				</form>
			</div>
			<div class="col-md-3 col-md-offset-1">
				<button type="button" class="btn btn-primary">
					<span class="glyphicon glyphicon-zoom-in"></span> 新增
				</button>
				<button type="button" class="btn btn-danger">
					<span class="glyphicon glyphicon-zoom-out"></span> 删除
				</button>
			</div>
		</div>

		<div class="row">
			<!-- 第三层布局, -->
			<div class="col-md-6 col-md-offset-3">
				<table class="table">
					<thead>
						<tr>
							<td>学号</td>
							<td>姓名</td>
							<td>性别</td>
							<td>家庭住址</td>
							<td>电话号码</td>
						</tr>
					</thead>
					<tbody>
						<c:forEach items="${pageInfo.list}" var="stu">
							<tr>
								<td>${stu.sBid}</td>
								<td>${stu.sName}</td>
								<td>${stu.sSex==1?'男':'女'}</td>
								<td>${stu.sAddr}</td>
								<td>${stu.sPhone}</td>
							</tr>
						
						</c:forEach>
					</tbody>
				</table>

			</div>
		</div>
		<div class="row">
			<!-- 第四层布局, -->
			<div class="col-md-2">

				总${pageInfo.pages}页,当前第${pageInfo.pageNum}页</div>

		</div>
		<div class="row">
			<!-- 第五层布局, -->
			<div class="col-md-4 col-md-offset-4">
				<nav aria-label="Page navigation">
				<ul class="pagination">
					<li><a href="#" aria-label="Previous"> <span
							aria-hidden="true">&laquo;</span>
					</a></li>

					<li><a href="#">1</a></li>
					<li><a href="#">2</a></li>
					<li><a href="#">3</a></li>
					<li><a href="#">4</a></li>
					<li><a href="#">5</a></li>
					<li><a href="#" aria-label="Next"> <span
							aria-hidden="true">&raquo;</span>
					</a></li>
				</ul>
				</nav>
			</div>

		</div>
	</div>
	<script
		src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
	<script
		src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"></script>

</body>
</html>

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值