将雇员按照部门分组显示
定义控制层的方法
public class EmpServlet extends BaseServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String pathInfo = req.getPathInfo();
if ("/add".equals(pathInfo)) {
this.addEmp(req, resp);
}else if ("/remove".equals(pathInfo)) {
this.removeEmp(req, resp);
}else if ("/get".equals(pathInfo)) {
this.getEmpById(req, resp);
}else if ("/list".equals(pathInfo)) {
this.getEmpList(req, resp);
}
}
/**
* 查询所有雇员的信息
* @param req
* @param resp
* @throws IOException
* @throws ServletException
*/
public void getEmpList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Map<String , Object> map = new HashMap<String, Object>();
//创建出10号部门的员工
List<Emp> list10 = new ArrayList<Emp>();
for (int i = 1; i <=5; i++) {
Emp emp = new Emp(1001, "尼古拉斯"+i, "总裁", 50000.00, new Date(), 1, 10000.00, 10);
list10.add(emp);
}
map.put("dept10", list10);
List<Emp> list20 = new ArrayList<Emp>();
for (int i = 1; i <=5; i++) {
Emp emp = new Emp(1002, "尼古拉斯"+i, "总裁", 50000.00, new Date(), 1, 10000.00, 10);
list20.add(emp);
}
map.put("dept20", list20);
List<Emp> list30 = new ArrayList<Emp>();
for (int i = 1; i <=5; i++) {
Emp emp = new Emp(1003, "尼古拉斯"+i, "总裁", 50000.00, new Date(), 1, 10000.00, 10);
list30.add(emp);
}
map.put("dept30", list30);
List<Emp> list40 = new ArrayList<Emp>();
for (int i = 1; i <=5; i++) {
Emp emp = new Emp(1004, "尼古拉斯"+i, "总裁", 50000.00, new Date(), 1, 10000.00, 10);
list40.add(emp);
}
map.put("dept40", list40);
req.setAttribute("empMap", map);
req.getRequestDispatcher("/emp_list.jsp").forward(req, resp);
}
JSP页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- 取得集合进行迭代,迭代到一条数据,就生成一行表格,每次迭代到一条键值对 -->
<c:forEach items="${empMap}" var="temp">
<h1>${temp.key}</h1>
<table border="1">
<tr>
<td>编号</td><td>姓名</td><td>职位</td><td>薪资</td><td>领导编号</td>
<td>入职日期</td><td>佣金</td><td>部门编号</td>
</tr>
<c:forEach items="${temp.value}" var="emp">
<tr>
<td>${emp.empno}</td><td>${emp.ename}</td><td>${emp.job}</td><td>${emp.sal}</td>
<td>${emp.mgr}</td><td>${emp.hiredate}</td><td>${emp.comm}</td><td>${emp.deptno}</td>
</tr>
</c:forEach>
</table>
</c:forEach>
</body>
</html>
根据职位查询雇员信息
调整业务层的实现类中的方法,增加一个分组的方法
@Override
public Map<String, Object> findAllSplit(String kw, Integer cp, Integer ls) {
Map<String, Object> map = new HashMap<String, Object>();
try {
/*将list集合中的雇员按照职位进行分组,以键值对的形式保存到map集合中,
key是职位名称,value是该职位下所有雇员的List集合 */
List<Emp> list = this.empDAO.selectSplitAll(kw, cp, ls);
//调用分组的方法
this.groupByJob(list, map);
//统计数据量
//map.put("count", this.empDAO.selectCount(kw));
} catch (Exception e) {
e.printStackTrace();
}finally {
C3P0Util.close(conn);
}
return map;
}
/**
* 按照职位进行分组
* @param list 保存了所有雇员的信息的集合
* @param map 最终要保存分组后的雇员的信息
*/
public void groupByJob(List<Emp> list,Map<String, Object> map) {
//对list进行迭代
Iterator<Emp> iter = list.iterator();
while (iter.hasNext()) {
Emp emp = iter.next();
//判断map集合中是否已经存在了当前雇员 的职位的集合
if (map.containsKey(emp.getJob())) {
//如果存在则直接保存当前雇员到这个职位对应的list集合中
List<Emp> emplist = (List<Emp>)map.get(emp.getJob());
emplist.add(emp);
}else {
//新建一个List集合,之后将第一个雇员保存到该集合中,之后再将该集合保存到Map集合
List<Emp> newList = new ArrayList<>();
newList.add(emp);
map.put(emp.getJob(), newList);
}
}
}
控制层方法
public void getEmpList(HttpServletRequest req, HttpServletResponse resp) {
IEmpService service = new EmpServiceImpl();
req.setAttribute("empMap", service.findAllSplit("", 1, 100));
try {
req.getRequestDispatcher("/emp_list.jsp").forward(req, resp);
} catch (Exception e) {
e.printStackTrace();
}
}
定义jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- 取得集合进行迭代,迭代到一条数据,就生成一行表格,每次迭代到一条键值对 -->
<c:forEach items="${empMap}" var="temp">
<h1>${temp.key}</h1>
<table border="1">
<tr>
<td>编号</td><td>姓名</td><td>职位</td><td>薪资</td><td>领导编号</td>
<td>入职日期</td><td>佣金</td><td>部门编号</td>
</tr>
<c:forEach items="${temp.value}" var="emp">
<tr>
<td>${emp.empno}</td><td>${emp.ename}</td><td>${emp.job}</td><td>${emp.sal}</td>
<td>${emp.mgr}</td><td>${emp.hiredate}</td><td>${emp.comm}</td><td>${emp.deptno}</td>
</tr>
</c:forEach>
</table>
</c:forEach>
</body>
</html>