保持热爱、奔赴山河
和传统 CRUD 一样,实现对员工信息的增删改查。
搭建环境
public class Employee {
private Integer id;
private String lastName;
private String email;
//1 male, 0 female
private Integer gender;
public Employee() {
}
public Employee(Integer id, String lastName, String email, Integer gender) {
this.id = id;
this.lastName = lastName;
this.email = email;
this.gender = gender;
}
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;
}
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", lastName='" + lastName + '\'' +
", email='" + email + '\'' +
", gender=" + gender +
'}';
}
}
@Repository
public class EmployeeDao {
private static Map<Integer, Employee> employees = null;
static {
employees = new HashMap<Integer, Employee>();
employees.put(1001, new Employee(1001, "E-AA", "aa@163.com", 1));
employees.put(1002, new Employee(1002, "E-BB", "bb@163.com", 1));
employees.put(1003, new Employee(1003, "E-CC", "cc@163.com", 0));
employees.put(1004, new Employee(1004, "E-DD", "dd@163.com", 0));
employees.put(1005, new Employee(1005, "E-EE", "ee@163.com", 1));
}
private static Integer initId = 1006;
public void save(Employee employee) {
if (employee.getId() == null) {
employee.setId(initId++);
}
employees.put(employee.getId(), employee);
}
public Collection<Employee> getAll() {
return employees.values();
}
public Employee get(Integer id) {
return employees.get(id);
}
public void delete(Integer id) {
employees.remove(id);
}
}
功能 | URL地址 | 请求方式 |
---|---|---|
访问首页√ | / | GET |
查询全部数据√ | /employee | GET |
删除√ | /employee/2 | DELETE |
跳转到添加数据页面√ | /toAdd | GET |
执行保存√ | /employee | POST |
跳转到更新数据页面√ | /employee/2 | GET |
执行更新√ | /employee | PUT |
<mvc:view-controller path="/" view-name="index"/>
DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" >
<title>Titletitle>
head>
<body>
<h1>首页h1>
<a th:href="@{/employee}">访问员工信息a>
body>
html>
// 查询所有的员工信息-->/employee-->GET
@GetMapping("/employee")
public String getAllEmployee(Model model){
// 查询所有的员工信息
Collection<Employee> employeeDaoAll = employeeDao.getAll();
// 将所有员工信息放在请求域中共享
model.addAttribute("employeeDaoAll",employeeDaoAll);
// 跳转到列表页面
return "employee_list";
}
DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Employee Infotitle>
<link rel="stylesheet" th:href="@{/static/css/index_work.css}">
head>
<body>
<div id="app">
<table>
<tr>
<th colspan="5">员工信息th>
tr>
<tr>
<th>idth>
<th>姓名th>
<th>邮箱th>
<th>性别th>
<th>选项(<a th:href="@{/to/add}">添加a> )th>
tr>
<tr th:each="employee:${employeeList}">
<td th:text="${employee.id}">td>
<td th:text="${employee.lastName}">td>
<td th:text="${employee.email}">td>
<td th:text="${employee.gender}">td>
<td>
<a th:href="@{'/employee/'+${employee.id}}">删除a>
<a @click="deleteEmployee" th:href="@{'/employee/'+${employee.id}}">更新a>
td>
tr>
table>
div>
body>
html>
<form id="delete_form" method="post">
<input type="hidden" name="_method" value="delete"/>
form>
引入vue.js
<script type="text/javascript" th:src="@{/static/js/vue.js}">script>
删除超链接
<a @click="deleteEmployee()" th:href="@{'/employee/'+${employee.id}}">删除a>
通过vue处理点击事件
<script type="text/javascript">
let vue = new Vue({
// 设置挂在容器
el:"#app",
methods:{
// event表示当前事件
deleteEmployee:function () {
//通过id获取表单数据
let delete_form = document.getElementById("delete_form");
//将超链接的href属性赋值给表单的action属性
// event.target(当前触发事件的标签)
delete_form.action = event.target.href;
//提交表单
delete_form.submit();
//阻止超链接的默认跳转行为
event.preventDefault();
}
}
});
script>
// 删除员工信息-->/employee/1---DELETE
@DeleteMapping("/employee/{id}")
public String deleteEmployeeById(@PathVariable("id") Integer id){
employeeDao.delete(id);
return "redirect:/employee";
}
方式一:控制器方法
// 跳转到添加页面-->/to/add-->GET
@GetMapping("/to/add")
public String toPageAdd() {
return "employee_add";
}
方式二:标签
<mvc:view-controller path="/to/add" view-name="employee_add">mvc:view-controller>
DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>添加员工title>
<link rel="stylesheet" th:href="@{/static/css/index_work.css}">
head>
<body>
<form th:action="@{/employee}" method="post">
<table>
<tr>
<th colspan="2">添加员工信息th>
tr>
<tr>
<td>姓名td>
<td><input type="text" name="lastName">td>
tr>
<tr>
<td>邮箱td>
<td><input type="text" name="email">td>
tr>
<tr>
<td>性别td>
<td>
<input type="radio" name="gender" value="1">男
<input type="radio" name="gender" value="2">女
td>
tr>
<tr>
<td colspan="2">
<input type="submit" value="添加">
td>
tr>
table>
form>
body>
html>
// 新增员工信息-->/employee-->POST
@PostMapping("/employee")
public String addEmployee(Employee employee){
employeeDao.save(employee);
return "redirect:/employee";
}
<a th:href="@{'/employee/'+${employee.id}}">修改a>
// 跳转到修改页面-->/employee/1-->GET
@GetMapping("/employee/{id}")
public String employeeById(@PathVariable("id") Integer id, Model model) {
Employee employee = employeeDao.get(id);
model.addAttribute("employee", employee);
return "employee_update";
}
DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>修改员工信息title>
<link rel="stylesheet" th:href="@{/static/css/index_work.css}">
head>
<body>
<form th:action="@{/employee}" method="post">
<input type="hidden" name="_method" value="put">
<input type="hidden" name="id" th:value="${employee.id}">
<table>
<tr>
<th colspan="2">修改员工信息th>
tr>
<tr>
<td>姓名td>
<td>
<input type="text" name="lastName" th:value="${employee.lastName}">
td>
tr>
<tr>
<td>邮箱td>
<td>
<input type="text" name="email" th:value="${employee.email}">
td>
tr>
<tr>
<td>性别td>
<td>
<input type="radio" name="gender" value="1" th:field="${employee.gender}">男
<input type="radio" name="gender" value="0" th:field="${employee.gender}">女
td>
tr>
<tr>
<td colspan="2">
<input type="submit" value="保存">
td>
tr>
table>
form>
body>
html>
// 修改员工信息-->/employee-->PUT
@PutMapping("/employee")
public String updateEmployee(Employee employee) {
employeeDao.save(employee);
return "redirect:/employee";
}
DispatcherServlet 用于处理浏览器向服务器发送的请求,因为处理请求路径url-pattern为/(jsp请求除外)。
<servlet>
<servlet-name>springmvcservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:springMVC.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>springmvcservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
当前访问静态资源请求也会被 DispatcherServlet 处理,但是 DispatcherServlet 不具备处理静态请求的能力。
在Tomcat中,配置文件xml内,有默认处理静态资源的配置 DefaultServlet ,处理请求路径为/ 。
<servlet>
<servlet-name>defaultservlet-name>
<servlet-class>org.apache.catalina.servlets.DefaultServletservlet-class>
<init-param>
<param-name>debugparam-name>
<param-value>0param-value>
init-param>
<init-param>
<param-name>listingsparam-name>
<param-value>falseparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>defaultservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
当前工程 web.xml 和 Tomcat 的 web.xml 关系为继承,如果有相同配置以当前工程为主。
此时,浏览器发送的请求会优先被DispatcherServlet进行处理,但是DsipatcherServlet无法处理静态资源配置。
使用SpringMVC的标签
<mvc:default-servlet-handler/>
<mvc:default-servlet-handler/>
<mvc:annotation-driven>mvc:annotation-driven>