实体类Employee和Department
Employee
属性字段:员工id,姓名lastName,邮箱email部门department
package com.lanou.demo.pojo;
import org.springframework.stereotype.Component;
@Component
public class Employee {
private Integer id;
private String lastName;
private String email;
private Integer gender;
private Department department;
public Employee() {
}
public Employee(Integer id, String lastName, String email, Integer gender, Department department) {
this.id = id;
this.lastName = lastName;
this.email = email;
this.gender = gender;
this.department = department;
}
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", lastName='" + lastName + '\'' +
", email='" + email + '\'' +
", gender=" + gender +
", department=" + department +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
}
Department
属性字段:部门id,部门名称name
package com.lanou.demo.pojo;
import org.springframework.stereotype.Component;
@Component
public class Department {
private Integer id;
private String name;
public Department(){
}
public Department(Integer id, String name) {
this.id = id;
this.name = name;
}
public Integer getId() {
return id;
}
@Override
public String toString() {
return "Department{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
需求1:查询所有员工(index.jsp)查询结果返回到另一个页面(list.jsp)
index.jsp页面,查询所有员工的请求路径:/emps
<a href="/emps">List All Employees</a>
dao层先进行查询操作
EmployeeDao.class
package com.lanou.demo.dao;
import com.lanou.demo.pojo.Department;
import com.lanou.demo.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
@Repository
public class EmployeeDao {
private static Map<Integer, Employee> employees = null;
@Autowired
private DepartmentDao departmentDao;
static {
employees = new HashMap<Integer, Employee>();
employees.put(1001, new Employee(1001, "张三", "[email protected]", 1, new Department(101, "教务部")));
employees.put(1002, new Employee(1002, "李四", "[email protected]", 1, new Department(102, "技术部")));
employees.put(1003, new Employee(1003, "王二麻子", "[email protected]", 0, new Department(103, "财务部")));
employees.put(1004, new Employee(1004, "小淘气", "[email protected]", 0, new Department(104, "总裁部")));
employees.put(1005, new Employee(1005, "谢霆锋", "[email protected]", 1, new Department(105, "安保部")));
}
private static Integer initId = 1006;
//查询所有员工
public Collection<Employee> getAll() {
return employees.values();
}
controller层
EmployeeHandler
@RequestMapping(value = "/emps",method = RequestMethod.GET)
获取jsp页面的请求路径/emps,查询所有员工,map.put装载查询结果key值为employees,通过return将数据返回给list.jsp页面
package com.lanou.demo.handlers;
import com.lanou.demo.dao.DepartmentDao;
import com.lanou.demo.dao.EmployeeDao;
import com.lanou.demo.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
@Controller
public class EmployeeHandler {
@Autowired
private EmployeeDao employeeDao;
@Autowired
private DepartmentDao departmentDao;
//获取所有员工
@RequestMapping(value = "/emps",method = RequestMethod.GET)
public String list(Map<String,Object> map){
map.put("employees",employeeDao.getAll());
return "list";
}
list.jsp页面
通过requestScope获取EmployeeHandler的map传来的employees的值
对数据进行判断,动态加载list.jsp的页面
<c:if test="${!empty requestScope.employees}">
遍历加载
<c:forEach items="${requestScope.employees}" var="emp">
<%--
Created by IntelliJ IDEA.
User: 吉新伟
Date: 2020/9/10
Time: 9:23
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<meta charset="UTF-8" content="text/html">
<title>Title</title>
</head>
<body>
<c:if test="${empty requestScope.employees}">
<%--没有员工信息--%>
</c:if>
<c:if test="${!empty requestScope.employees}">
<table border="1" cellpadding="10" cellspacing="0">
<tr>
<th>ID</th>
<th>LastName</th>
<th>Email</th>
<th>Gender</th>
<th>Department</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<c:forEach items="${requestScope.employees}" var="emp">
<tr>
<td>${emp.id}</td>
<td>${emp.lastName}</td>
<td>${emp.email}</td>
<td>${emp.gender==0?'Female':"Male"}</td>
<td>${emp.department.name}</td>
<td><a href="/emp/${emp.id}">Edit</a></td>
<td><form action="/emp/${emp.id}" method="post">
<input type="hidden" name="_method" value="DELETE" /><input type="submit" value="delete"> </form></td>
</tr>
</c:forEach>
</table>
</c:if>
</body>
</html>
需求2 增加员工
<a href="/emp">2 Add new employee</a>
<br><br>
在增加员工的input2.jsp页面
需要动态的返回员工部门信息
Department: <form:select path="department.name"
items="${departments }" itemLabel="name" itemValue="name"></form:select>
因此在添加员工之前,首先要通过map.put将部门信息返回
input.jsp
<%@page import="java.util.HashMap" %>
<%@page import="java.util.Map" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ 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>
<!--
1. 为什么 使用 form 标签呢 ?
可以更快速的开发出表单页面, 而且可以更方便的进行表单值的回显
2. 注意:
可以通过 modelAttribute 属性指定绑定的模型属性,
若没有指定该属性,则默认从 request 域对象中读取 command 的表单 bean
如果该属性值也不存在,则会发生错误。
-->
<br><br>
<form:form action="/emp" method="POST" modelAttribute="employee">
<c:if test="${employee.id == null}">
<!-- 1 path 属性对应 html 表单标签的 name 属性值 -->
LastName: <form:input path="lastName"/><br>
</c:if>
<c:if test="${employee.id != null }">
<!--修改-->
<form:hidden path="id" />
<input type="hidden" name="_method" value="PUT" />
</c:if>
<br>
Email: <form:input path="email" autocomplete="off"/>
<br>
<%
Map<String, String> genders = new HashMap();
genders.put("1", "Male");
genders.put("0", "Female");
request.setAttribute("genders", genders);
%>
Gender:
<br>
<form:radiobuttons path="gender" items="${genders }"/>
<br>
Department: <form:select path="department.name"
items="${departments }" itemLabel="name" itemValue="name"></form:select>
<br>
<input type="submit" value="Submit"/>
</form:form>
</body>
</html>
返回部门信息的操作步骤
1dao层
DepartmentDao
package com.lanou.demo.dao;
import com.lanou.demo.pojo.Department;
import org.springframework.stereotype.Repository;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
@Repository
public class DepartmentDao {
private static Map<Integer, Department> departments= null;
static{
departments = new HashMap<Integer, Department>();
departments.put(101, new Department(101, "HR Department"));
departments.put(102, new Department(102, "IT Department"));
departments.put(103, new Department(103, "Finance Department"));
departments.put(104, new Department(104, "Cleaning Department"));
departments.put(105, new Department(105, "Security Department"));
}
//返回部门信息
public Collection<Department> getDepartments() {
return departments.values();
}
}
2Controller层
EmployeeHandler
根据/emp请求将部门信息Department返回到input2.jsp
//返回input.jsp页面的部门信息
@RequestMapping(value = "/emp",method = RequestMethod.GET)
public String input(Map<String,Object> map){
map.put("departments",departmentDao.getDepartments());
// 第一次默认回显,form标签
map.put("employee",new Employee());
return "input2";
}
此时根据input2.jsp页面的表单提交路径/emp,method=post来添加员工
dao层
方法:save(Employee employee)
package com.lanou.demo.dao;
import com.lanou.demo.pojo.Department;
import com.lanou.demo.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
@Repository
public class EmployeeDao {
private static Map<Integer, Employee> employees = null;
@Autowired
private DepartmentDao departmentDao;
static {
employees = new HashMap<Integer, Employee>();
employees.put(1001, new Employee(1001, "张三", "[email protected]", 1, new Department(101, "教务部")));
employees.put(1002, new Employee(1002, "李四", "[email protected]", 1, new Department(102, "技术部")));
employees.put(1003, new Employee(1003, "王二麻子", "[email protected]", 0, new Department(103, "财务部")));
employees.put(1004, new Employee(1004, "小淘气", "[email protected]", 0, new Department(104, "总裁部")));
employees.put(1005, new Employee(1005, "谢霆锋", "[email protected]", 1, new Department(105, "安保部")));
}
private static Integer initId = 1006;
//查询所有员工
public Collection<Employee> getAll() {
return employees.values();
}
//添加员工
public void save(Employee employee) {
if (employee.getId() == null) {
employee.setId(initId++);
}
employees.put(employee.getId(), employee);
}
}
EmployeeHandler
参数employee的属性时根根据index.jsp的form标签中的path属性自动赋值
employee的属性:lastName,email,gender,department
input2.jsp中对应的值(path):
LastName: <form:input path="lastName"/><br>
Email: <form:input path="email" autocomplete="off"/>
<form:radiobuttons path="gender" items="${genders }"/>
Department: <form:select path="department.name"
items="${departments }" itemLabel="name" itemValue="name"></form:select>
//新增
@RequestMapping(value = "/emp",method = RequestMethod.POST)
public String save(Employee employee){
employeeDao.save(employee);
return "redirect:/emps";
}
需求3 修改员工
dao层
在修改时进行判断,如果id不存在则生成一个id进行添加,如果id存在则还是原来那个id进行添加
public void save(Employee employee) {
if (employee.getId() == null) {
employee.setId(initId++);
}
employees.put(employee.getId(), employee);
}
controller层
@ModelAttribute注解对应的方法对input2.jsp中mo,根据employee的id属性对数据进行查询,如果查到数据则传到input2.jsp中
前端页面/emp/${emp.id}会发一个restful请求,
<td><a href="/emp/${emp.id}">Edit</a></td>
controller层通过 @RequestMapping(value="/emp/{id}", method=RequestMethod.GET)接收请求并把数据返回到input2.jsp所需的位置
@ModelAttribute
public void getEmployee(@RequestParam(value = "id",required = false)Integer id,Map<String,Object>map){
if (id!=null){
map.put("employee", employeeDao.get(id));
}
}
//修改
@RequestMapping(value="/emp/{id}", method=RequestMethod.GET)
public String input(@PathVariable("id") Integer id ,Map<String, Object> map){
map.put("departments", departmentDao.getDepartments());
map.put("employee", employeeDao.get(id));
return "input2";
}
@RequestMapping(value="/emp", method=RequestMethod.PUT)
public String update(Employee employee){
employeeDao.save(employee);
return "redirect:/emps";
}
需求4 删除员工
EmployeeDao
public void delete(Integer id){
employees.remove(id);
}
EmployeeHandler
//删除
@RequestMapping(value="/emp/{id}", method=RequestMethod.DELETE)
public String deleteEmp(@PathVariable("id") Integer id){
System.out.println(id);
employeeDao.delete(id);
return "redirect:/emps";
}
<%--
Created by IntelliJ IDEA.
User: 吉新伟
Date: 2020/9/10
Time: 9:23
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<script type="text/javascript" src="scripts/jquery-1.9.1.min.js"></script>
<meta charset="UTF-8" content="text/html">
<title>Title</title>
</head>
<body>
<c:if test="${empty requestScope.employees}">
<%--没有员工信息--%>
</c:if>
<c:if test="${!empty requestScope.employees}">
<table border="1" cellpadding="10" cellspacing="0">
<tr>
<th>ID</th>
<th>LastName</th>
<th>Email</th>
<th>Gender</th>
<th>Department</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<c:forEach items="${requestScope.employees}" var="emp">
<tr>
<td>${emp.id}</td>
<td>${emp.lastName}</td>
<td>${emp.email}</td>
<td>${emp.gender==0?'Female':"Male"}</td>
<td>${emp.department.name}</td>
<td><a href="/emp/${emp.id}">Edit</a></td>
<td><a href="/emp/${emp.id}" class="delete">Delete</a> </td>
</tr>
</c:forEach>
</table>
</c:if>
<form method="post" action="">
<input type="hidden" name="_method" value="DELETE" />
</form>
<a href="/emp">2 Add new employee</a>
<br><br>
<script>
$(function () {
$(".delete").click(function () {
var href = $(this).attr("href");
$("form").attr("action", href).submit();
return false;
});
});
</script>
<br><br>
</body>
</html>
动态的为form添加action,发送delete请求