功能一:查询员工数据

  1. 访问index.jsp页面

  2. index.jsp页面发送出查询员工列表请求

  3. EmployeeController来接受请求,查出员工数据

  4. 来到list.jsp页面进行展示

  • URI: /emps

效果图:
avatar

测试

首先分析一下,这个页面一页有5条数据,规定了URI为/emps,可以分页查询到所有数据

那么首先就在index.jsp中编写

1
<jsp:forward page="//emps"></jsp:forward>

访问首页时就会转到/emps

跟随逻辑,index.jsp发送列表请求被Controller接受并查出员工数据

EmployeeController

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.ceit.crud.controller;

import com.ceit.crud.bean.Employee;
import com.ceit.crud.service.EmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
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 java.util.List;

/**
* 处理员工CRUD请求
*
*/
@Controller
public class EmployeeController {

@Autowired
EmployeeService employeeService;
/**
* 查询员工数据(分页查询)
* @return
*/

@RequestMapping("/emps")
public String getEmps(@RequestParam(value = "pn",defaultValue = "1")Integer pn, Model model){
//这不是一个分页查询
//引入PageHelper分页插件
//在查询之前只需要调用,传入页码以及每页的个数
PageHelper.startPage(pn,5);
//startPage后面紧跟的这个查询就是一个分页查询
List<Employee> emps = employeeService.getAll();
//使用PageInfo包装查询后的结果,只需要将PageInfo交给页面就行了
//封装了详细的分页信息,包括有我们查询出来的数据,连续显示的页数
PageInfo page= new PageInfo(emps,5);
model.addAttribute("pageInfo",page);
page.getNavigatepageNums();
return "list";
}
}

这里使用了PageHelper分页插件来进行分页,具体百度就行

pn就是页码,默认值为1

先暂时不放在页面上,所以对其进行一下单元测试

在test包下新建一个MvcTest

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.ceit.crud.test;

import com.ceit.crud.bean.Employee;
import com.github.pagehelper.PageInfo;
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.MockMvcBuilder;
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 java.util.List;

/**
* 使用Spring测试模块提供的测试请求功能,测试crud请求的正确性
* Spring4测试的时候,需要servlet3的支持
*/


@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations={"classpath:applicationContext.xml","file:src/main/webapp/WEB-INF/dispatcherServlet-servlet.xml"})
public class MVCTest {
//传入Springmvc的ioc
@Autowired
WebApplicationContext context;
//虚拟mvc请求,获取到处理结果
MockMvc mockMvc;

@Before
public void initMokcMvc(){
mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
}

@Test
public void testPage() throws Exception {
//模拟请求拿到返回值
MvcResult result = mockMvc.perform(MockMvcRequestBuilders.get("/emps").param("pn", "5")).andReturn();

//请求成功以后,请求域中会有PageInfo,我们可以取出PageInfo进行验证
MockHttpServletRequest request = result.getRequest();
PageInfo pi = (PageInfo) request.getAttribute("pageInfo");
System.out.println("当前页码:"+pi.getPageNum());
System.out.println("总页码:"+pi.getPages());
System.out.println("总记录数"+pi.getTotal());
System.out.println("在页面需要连续显示的页码");
int[] nums = pi.getNavigatepageNums();
for(int i:nums){
System.out.print(" "+i);
}
System.out.println();
//获取员工数据
List<Employee> list = pi.getList();
for(Employee employee : list){
System.out.println("ID"+employee.getEmpId()+"==>Name"+employee.getEmpName());
}
}
}

这里踩了个坑,要注意的是pom.xml中的c3p0可能会因为版本问题报错,修改成
1
2
3
4
5
6
7
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.5</version>
<type>jar</type>
<scope>compile</scope>
</dependency>

重启加载依赖即可

运行结果

1
2
3
4
5
6
7
8
9
10
当前页码:5
总页码:250
总记录数1248
在页面需要连续显示的页码
3 4 5 6 7
ID21==>Namedffa319
ID22==>Name31b4020
ID23==>Name4fc7c21
ID24==>Name9b3c422
ID25==>Namef878c23

这里看到当前页码,总页码,总记录数,需要连续显示的页码以及数据都已经能成功捕获到了,那么接下来只需要写页面进行展示即可