8. RESTful案例
阅读原文时间:2023年09月01日阅读:3

1. 准备工作

和传统 CRUD 一样,实现对员工信息的增删改查。

  • 搭建环境

  • 准备实体类

    package com.atguigu.mvc.bean;
    public class Employee {
       private Integer id;
       private String lastName;
       private String email;
       //1 male, 0 female
       private Integer 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;
      }
       public Employee(Integer id, String lastName, String email, Integergender) {
           super();
           this.id = id;
           this.lastName = lastName;
           this.email = email;
           this.gender = gender;
      }
       public Employee() {
      }
    }

  • 准备dao模拟数据

    package com.atguigu.mvc.dao;
    import java.util.Collection;
    import java.util.HashMap;
    import java.util.Map;
    import com.atguigu.mvc.bean.Employee;
    import org.springframework.stereotype.Repository;
    @Repository
    public class EmployeeDao {
       private static Map employees = null;
       static{
           employees = new HashMap();
           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 getAll(){
           return employees.values();
      }
       public Employee get(Integer id){
           return employees.get(id);
      }
       public void delete(Integer id){
           employees.remove(id);
      }
    }

2. 功能清单

功能

URL 地址

请求方式

访问首页√

/

GET

查询全部数据√

/employee

GET

删除√

/employee/2

DELETE

跳转到添加数据页面√

/toAdd

GET

执行保存√

/employee

POST

跳转到更新数据页面√

/employee/2

GET

执行更新√

/employee

PUT

3. 具体功能:访问首页

①配置view-controller

<mvc:view-controller path="/" view-name="index"/>

②创建页面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
 &nbsp; &nbsp;<head>
 &nbsp; &nbsp; &nbsp; &nbsp;<meta charset="UTF-8" >
 &nbsp; &nbsp; &nbsp; &nbsp;<title>Title</title>
 &nbsp; &nbsp;</head>
 &nbsp; &nbsp;<body>
 &nbsp; &nbsp; &nbsp; &nbsp;<h1>首页</h1>
 &nbsp; &nbsp; &nbsp; &nbsp;<a th:href="@{/employee}">访问员工信息</a>
 &nbsp; &nbsp;</body>
</html>

4. 具体功能:查询所有员工数据

①控制器方法

@RequestMapping(value = "/employee", method = RequestMethod.GET)
public String getEmployeeList(Model model){
 &nbsp; &nbsp;Collection<Employee> employeeList = employeeDao.getAll();
 &nbsp; &nbsp;model.addAttribute("employeeList", employeeList);
 &nbsp; &nbsp;return "employee_list";
}

②创建employee_list.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
 &nbsp; &nbsp;<head>
 &nbsp; &nbsp; &nbsp; &nbsp;<meta charset="UTF-8">
 &nbsp; &nbsp; &nbsp; &nbsp;<title>Employee Info</title>
 &nbsp; &nbsp; &nbsp; &nbsp;<script type="text/javascript" th:src="@{/static/js/vue.js}"></script>
 &nbsp; &nbsp;</head>
 &nbsp; &nbsp;<body>
 &nbsp; &nbsp; &nbsp; &nbsp;<table border="1" cellpadding="0" cellspacing="0" style="text-align:center;" id="dataTable">
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<tr>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<th colspan="5">Employee Info</th>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</tr>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<tr>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<th>id</th>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<th>lastName</th>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<th>email</th>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<th>gender</th>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<th>options(<a th:href="@{/toAdd}">add</a>)</th>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</tr>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<tr th:each="employee : ${employeeList}">
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<td th:text="${employee.id}"></td>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<td th:text="${employee.lastName}"></td>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<td th:text="${employee.email}"></td>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<td th:text="${employee.gender}"></td>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<td>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a class="deleteA" @click="deleteEmployee"
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; th:href="@{'/employee/'+${employee.id}}">delete</a>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a th:href="@{'/employee/'+${employee.id}}">update</a>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</td>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</tr>
 &nbsp; &nbsp; &nbsp; &nbsp;</table>
 &nbsp; &nbsp;</body>
</html>

5. 具体功能:删除

①创建处理delete请求方式的表单

<!-- 作用:通过超链接控制表单的提交,将post请求转换为delete请求 -->
<form id="delete_form" method="post">
 &nbsp; &nbsp;<!-- HiddenHttpMethodFilter要求:必须传输_method请求参数,并且值为最终的请求方式 -->
 &nbsp; &nbsp;<input type="hidden" name="_method" value="delete"/>
</form>

引入vue.js

<script type="text/javascript" th:src="@{/static/js/vue.js}"></script>

删除超链接

<a class="deleteA" @click="deleteEmployee"th:href="@{'/employee/'+${employee.id}}">delete</a>

通过vue处理点击事件

<script type="text/javascript">
 &nbsp; &nbsp;var vue = new Vue({
 &nbsp; &nbsp; &nbsp; &nbsp;el:"#dataTable",
 &nbsp; &nbsp; &nbsp; &nbsp;methods:{
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//event表示当前事件
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;deleteEmployee:function (event) {
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//通过id获取表单标签
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var delete_form = document.getElementById("delete_form");
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//将触发事件的超链接的href属性为表单的action属性赋值
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;delete_form.action = event.target.href;
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//提交表单
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;delete_form.submit();
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//阻止超链接的默认跳转行为
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;event.preventDefault();
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }
 &nbsp; &nbsp; &nbsp;  }
 &nbsp;  });
</script>

③控制器方法

@RequestMapping(value = "/employee/{id}", method = RequestMethod.DELETE)
public String deleteEmployee(@PathVariable("id") Integer id){
 &nbsp; &nbsp;employeeDao.delete(id);
 &nbsp; &nbsp;return "redirect:/employee";
}

6. 具体功能:跳转到添加数据页面

①配置view-controller

<mvc:view-controller path="/toAdd" view-name="employee_add"></mvc:view-controller>

②创建employee_add.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
 &nbsp; &nbsp;<head>
 &nbsp; &nbsp; &nbsp; &nbsp;<meta charset="UTF-8">
 &nbsp; &nbsp; &nbsp; &nbsp;<title>Add Employee</title>
 &nbsp; &nbsp;</head>
 &nbsp; &nbsp;<body>
 &nbsp; &nbsp; &nbsp; &nbsp;<form th:action="@{/employee}" method="post">
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  lastName:<input type="text" name="lastName"><br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  email:<input type="text" name="email"><br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  gender:<input type="radio" name="gender" value="1">male
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<input type="radio" name="gender" value="0">female<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<input type="submit" value="add"><br>
 &nbsp; &nbsp; &nbsp; &nbsp;</form>
 &nbsp; &nbsp;</body>
</html>

7. 具体功能:执行保存

①控制器方法

@RequestMapping(value = "/employee", method = RequestMethod.POST)
public String addEmployee(Employee employee){
 &nbsp; &nbsp;employeeDao.save(employee);
 &nbsp; &nbsp;return "redirect:/employee";
}

8. 具体功能:跳转到更新数据页面

①修改超链接

<a th:href="@{'/employee/'+${employee.id}}">update</a>

②控制器方法

@RequestMapping(value = "/employee/{id}", method = RequestMethod.GET)
public String getEmployeeById(@PathVariable("id") Integer id, Model model){
 &nbsp; &nbsp;Employee employee = employeeDao.get(id);
 &nbsp; &nbsp;model.addAttribute("employee", employee);
 &nbsp; &nbsp;return "employee_update";
}

③创建employee_update.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
 &nbsp; &nbsp;<head>
 &nbsp; &nbsp; &nbsp; &nbsp;<meta charset="UTF-8">
 &nbsp; &nbsp; &nbsp; &nbsp;<title>Update Employee</title>
 &nbsp; &nbsp;</head>
 &nbsp; &nbsp;<body>
 &nbsp; &nbsp; &nbsp; &nbsp;<form th:action="@{/employee}" method="post">
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<input type="hidden" name="_method" value="put">
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<input type="hidden" name="id" th:value="${employee.id}">
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  lastName:<input type="text" name="lastName" th:value="${employee.lastName}">
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  email:<input type="text" name="email" th:value="${employee.email}"><br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<!--
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;th:field="${employee.gender}"可用于单选框或复选框的回显
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;若单选框的value和employee.gender的值一致,则添加checked="checked"属性
            -->
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  gender:<input type="radio" name="gender" value="1"th:field="${employee.gender}">male
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<input type="radio" name="gender" value="0"th:field="${employee.gender}">female<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<input type="submit" value="update"><br>
 &nbsp; &nbsp; &nbsp; &nbsp;</form>
 &nbsp; &nbsp;</body>
</html>

9. 具体功能:执行更新

①控制器方法

@RequestMapping(value = "/employee", method = RequestMethod.PUT)
public String updateEmployee(Employee employee){
 &nbsp; &nbsp;employeeDao.save(employee);
 &nbsp; &nbsp;return "redirect:/employee";
}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章