查询员工列表(无数据库)
**说明:**该操作不涉及数据库,是将数据存储在前端页面request
域中,前端页面直接获取的。
结构目录:
步骤:
- 前端页面(index.js)直接发送
/emps
请求。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!-- 访问项目就要展示员工列表页面 -->
<jsp:forward page="/emps"></jsp:forward>>
- 控制器
EmployeeController.java
拦截/emps
请求,拦截后将employeeDao
中的数据保存到前端页面list.jsp
的request
域中,以便该页面能直接获取数据。
package com.atguigu.controller;
import java.util.Collection;
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 com.atguigu.bean.Employee;
import com.atguigu.dao.EmployeeDao;
@Controller
public class EmployeeController {
@Autowired
EmployeeDao employeeDao;
/*
* 查询所有员工
* */
@RequestMapping(value="/emps")
public String getEmps(Model model){
Collection<Employee> all = employeeDao.getAll();
model.addAttribute("emps", all);
return "list";
}
}
list
页面中获取reques域中的数据,并展示出来。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>员工列表</title>
</head>
<body>
<h1>员工列表</h1>
<table>
<tr>
<th>ID</th>
<th>lastName</th>
<th>lastName</th>
<th>email</th>
<th>gender</th>
<th>departmentName</th>
<th>EDIT</th>
<th>DELETE</th>
</tr>
<c:forEach items="${emps}" var="emp">
<tr>
<td>${emp.id}</td>
<td>${emp.lastName}</td>
<td>${emp.email}</td>
<td>${emp.gender==0?"女":"男"}</td>
<td>${emp.department.departmentName}</td>
<td>EDIT</td>
<td>DELETE</td>
</tr>
</c:forEach>
</table>
</body>
</html>