• 8 RESTful案例


    8、RESTful案例

    8.1、准备工作

    Employee类:

    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, Integer
                gender) {
            super();
            this.id = id;
            this.lastName = lastName;
            this.email = email;
            this.gender = gender;
        }
        public Employee() {
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    EmployeeDao类:

    @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);
        }
    
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    IDEA设置自动导包:
    在这里插入图片描述

    8.2、功能清单

    功能URL 地址请求方式
    访问首页√/GET
    查询全部数据√/employeeGET
    删除√/employee/2DELETE
    跳转到添加数据页面√/toAddGET
    执行保存√/employeePOST
    跳转到更新数据页面√/employee/2GET
    执行更新√/employeePUT

    8.3、具体功能:访问首页

    ①配置view-controller
    <mvc:view-controller path="/" view-name="index"/>
    
    • 1
    ②创建页面
    <!DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
    <meta charset="UTF-8" >
    <title>Title</title>
    </head>
    <body>
    <h1>首页</h1>
    <a th:href="@{/employee}">访问员工信息</a>
    </body>
    </html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    8.4、具体功能:查询所有员工数据(列表)

    ①控制器方法
    @Controller
    public class EmployeeController {
    
        @Autowired
        private EmployeeDao employeeDao;
    
    
        @GetMapping("/employee")
        public String getAllEmployee(Model model){
            //获取所有的员工信息
            Collection<Employee> all = employeeDao.getAll();
            //将所有员工信息在请求域中共享
            model.addAttribute("allEmployee",all);
            return "employee_list";
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    在这里插入图片描述
    扫描不到,需要更改:

        
        <context:component-scan base-package="com.gao"/>
    
    • 1
    • 2
        @GetMapping("/employee")
        public String getAllEmployee(Model model){
            //获取所有的员工信息
            Collection<Employee> all = employeeDao.getAll();
            //将所有员工信息在请求域中共享
            model.addAttribute("allEmployee",all);
            return "employee_list";
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    新建html(employee_list):

    <table>
        <tr>
            <th colspan="5">employee_listth>
        tr>
        <tr>
            <th>idth>
            <th>lastNameth>
            <th>emailth>
            <th>genderth>
            <th>optionsth>
        tr>
        <tr th:each="employee : ${allEmployee}">
            <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 href="">updatea>
                <a href="">deletea>
            td>
        tr>
    table>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    处理静态资源

    static静态资源:
    在这里插入图片描述
    引入资源:

    <head>
        <meta charset="UTF-8">
        <title>employee_listtitle>
        <link rel="stylesheet" th:href="@{/static/css/index.css}">
    head>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    配置

        
    
    
        
        <mvc:default-servlet-handler/>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    8.5、具体功能:删除

    使用Vue.js
    通过vue处理点击事件

    <div id="app">
    
        <table>
            <tr>
                <th colspan="5">employee_listth>
            tr>
            <tr>
                <th>idth>
                <th>lastNameth>
                <th>emailth>
                <th>genderth>
                <th>options(<a th:href="@{/to/add}">adda>)th>
            tr>
            <tr th:each="employee : ${allEmployee}">
                <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 @click="deleteEmployee"
                       th:href="@{'/employee/' + ${employee.id}}">deletea>
                    <a th:href="@{'/employee/' + ${employee.id}}">updatea>
                td>
            tr>
        table>
    
        <form  id="delete_form" method="post">
            <input type="hidden" name="_method" value="delete">
        form>
    
    div>
    
    
    <script type="text/javascript" th:src="@{/static/js/vue.js}">script>
    <script type="text/javascript">
        var vue = new Vue({
            el:"#app",
            methods:{
    //event表示当前事件
                deleteEmployee:function (event) {
    //通过id获取表单标签
                    var delete_form = document.getElementById("delete_form");
    //将触发事件的超链接的href属性为表单的action属性赋值
                    delete_form.action = event.target.href;
    //提交表单
                    delete_form.submit();
    //阻止超链接的默认跳转行为
                    event.preventDefault();
                }
            }
        });
    
    script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
        @DeleteMapping("/employee/{id}")
        public String deleteEmployee(@PathVariable("id") Integer id){
            //删除员工信息
            employeeDao.delete(id);
            //重定向到列表
            return "redirect:/employee";
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    8.6、具体功能:添加数据

    实现跳转到添加数据页面:

    <th>options(<a th:href="@{/to/add}">adda>)th>
    
    • 1

    springmvc.xml配置:

        <mvc:view-controller path="/" view-name="index">mvc:view-controller>
        <mvc:view-controller path="/to/add" view-name="employee_add">mvc:view-controller>
    
    • 1
    • 2

    employee_add.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>employee_add</title>
    </head>
    <body>
        <form th:action="@{/employee}" method="post">
            lastName:<input type="text" name="lastName"><br>
            email:<input type="text" name="email"><br>
            gender:<input type="radio" name="gender" value="1">male
            <input type="radio" name="gender" value="0">female<br>
            <input type="submit" value="add"><br>
        </form>
    </body>
    </html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    重定向跳转:

        @PostMapping("/employee")
        public String addEmployee(Employee employee){
            //保存员工信息
            employeeDao.save(employee);
    
            return "redirect:/employee";
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    8.7、具体功能:修改数据

    update(注意格式

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

    页面跳转:

        @GetMapping("/employee/{id}")
        public String toUpdate(@PathVariable("id") Integer id,Model model){
            //根据id查询员工信息
            Employee employee = employeeDao.get(id);
            //将员工信息共享到请求域中
            model.addAttribute("employee",employee);
            //跳转到employee_update.html
            return "employee_update";
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    employee_update页面:

    
    <form th:action="@{/employee}" method="post">
        <input type="hidden" name="_method" value="put">
        <input type="hidden" name="id" th:value="${employee.id}">
        lastName:<input type="text" name="lastName" th:value="${employee.lastName}">
        <br>
        email:<input type="text" name="email" th:value="${employee.email}"><br>
        
        gender:<input type="radio" name="gender" value="1"
                      th:field="${employee.gender}">male
        <input type="radio" name="gender" value="0"
               th:field="${employee.gender}">female<br>
        <input type="submit" value="update"><br>
    form>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    update操作:

        @PutMapping("/employee")
        public String updateEmployee(Employee employee){
            //修改员工信息
            employeeDao.save(employee);
            return "redirect:/employee";
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 相关阅读:
    Cilium系列-15-7层网络CiliumNetworkPolicy简介
    深度学习二三事-计算机视觉目标检测回顾
    API cop
    重写muduo网络库开发环境和前期准备
    ShardingSphere实现数据库读写分离,主从库分离,docker详细教程
    快排+归并非递归实现
    【vue】组件通选方式
    uniapp项目笔记
    时间序列(三):多变量回归
    用 Java?就用国产生态型应用开发框架:Solon v1.10.2
  • 原文地址:https://blog.csdn.net/qq_44774198/article/details/126177288