zoukankan      html  css  js  c++  java
  • 13、SpringBoot-CRUD员工修改操作/删除

     
    对于修改连接的uri
    在list.html中
    <a class="btn btn-sm btn-primary" th:href="@{/update/} + ${emp.id} ">修改</a>
    修改需要知道id,所以路径上需要有有该修改的员工id
    两个属性是要进行拼串的不可以写在一起

     controller实现页面的跳转

    //修改
    @GetMapping("/update/{id}")
    public String updataEmp(@PathVariable("id")Integer id,
                            Model model){
    
        Employee employee = employeeDao.get(id);
        model.addAttribute("emp",employee);
    
        //部门选择的修改
        Collection<Department> departments = departmentDao.getDepartments();
        model.addAttribute("depts",departments);
    
        return "emp/update";
    }
    在add.html文件夹中复制并且命名为update.html
     
    注意使用的RESTFUL方式,使用put请求时的隐藏域以及id的隐藏域
    对数据进行的回显判断操作
    <form th:action="@{/update1}" method="post">
       <!--发送put请求修改员工数据-->
       <!--
       1、SpringMVC中配置HiddenHttpMethodFilter;(SpringBoot自动配置好的)
       2、页面创建一个post表单
       3、创建一个input项,name="_method";值就是我们指定的请求方式
       -->
       <input type="hidden" name="_method" value="put" th:if="${emp!=null}"/>
       <!-- id -->
       <input type="hidden" name="id" th:if="${emp!=null}" th:value="${emp.id}">
    
       <div class="form-group">
          <label>LastName</label>
          <input name="lastName" type="text" class="form-control" 
    placeholder="zhangsan" th:value="${emp!=null}?${emp.lastName}"> </div> <div class="form-group"> <label>Email</label> <input name="email" type="email" class="form-control"
    placeholder="zhangsan@atguigu.com" th:value="${emp!=null}?${emp.email}"> </div> <div class="form-group"> <label>Gender</label><br/> <div class="form-check form-check-inline"> <input class="form-check-input" type="radio"
    name="gender" value="1" th:checked="${emp!=null}?${emp.gender==1}"> <label class="form-check-label">男</label> </div> <div class="form-check form-check-inline"> <input class="form-check-input" type="radio" name="gender" value="0" th:checked="${emp!=null}?${emp.gender==0}"> <label class="form-check-label">女</label> </div> </div> <div class="form-group"> <label>department</label> <!--提交的是部门的id--> <select class="form-control" name="department.id"> <option th:each="dept:${depts}" th:text="${dept.departmentName}" th:selected="${dept.id == emp.department.id}" th:value="${dept.id}">1</option> </select> </div> <div class="form-group"> <label>Birth</label> <input name="birth" type="text" class="form-control"
    placeholder="zhangsan" th:value="${emp!=null}?${#dates.format(emp.birth, 'yyyy-MM-dd HH:mm')}"> </div> <button type="submit" class="btn btn-primary" th:text="${emp!=null}?'修改':'添加'">添加</button> </form>

    实现页面修改的controller:

    //员工修改
    @PutMapping("/update1")
    public String  updataToEmp(Employee employee){
    
        System.out.println(employee);
    
        //修改的数据
        employeeDao.save(employee);
    
        return "redirect:/emps";
    }
    此时可以成功添加数据  修改数据
    在删除页面的标志:

    list.html中

    <form th:action="@{/delete/}+${emp.id}" method="post">
       <input type="hidden" name="_method" value="delete">
       <button class="btn btn-sm btn-danger">删除</button>
    </form>

     controller实现:

    //删除请求
    @DeleteMapping("/delete/{id}")
    public  String  delete(@PathVariable("id") Integer id){
        employeeDao.delete(id);
    
        return "redirect:/emps";
    }
  • 相关阅读:
    PyQuery基本操作介绍
    JuPyter(IPython) Notebook中通过pip安装第三方Python Module
    PyQuery查询html信息
    Windows10 磁盘活动时间百分之百导致系统卡顿解决方法
    Django中文无法转换成latin-1编码的解决方案
    Spring Security核心概念介绍
    正则表达式之基本原理
    java基础类型源码解析之HashMap
    java基础类型源码解析之String
    java集合类型源码解析之PriorityQueue
  • 原文地址:https://www.cnblogs.com/Mrchengs/p/10356983.html
Copyright © 2011-2022 走看看