Spring boot修改员工

修改员工

点击修改按钮,根据用户id
查询用户信息,查询所有的部门列表信息

回显到修改页面
点击确认,提交用户信息

用户列表页面

<main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4">
    <h2><a class="btn btn-sm btn-success" href="emp" th:href="@{/emp}">员工添加</a></h2>
    <div class="table-responsive">
        <table class="table table-striped table-sm">
            <thead>
            <tr>
                <th>#</th>
                <th>lastName</th>
                <th>email</th>
                <th>gender</th>
                <th>department</th>
                <th>birth</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            <tr th:each="emp:${emps}">
                <td th:text="${emp.id}"></td>
                <td>[[${emp.lastName}]]</td>
                <td th:text="${emp.email}"></td>
                <td th:text="${emp.gender}==0?'女':'男'"></td>
                <td th:text="${emp.department.departmentName}"></td>
                <td th:text="${#dates.format(emp.birth, 'yyyy-MM-dd HH:mm')}"></td>
                <td>
                    <a class="btn btn-sm btn-primary" th:href="@{/emp/}+${emp.id}">编辑</a>
                    <button th:attr="del_uri=@{/emp/}+${emp.id}" class="btn btn-sm btn-danger deleteBtn">删除
                    </button>
                </td>
            </tr>
            </tbody>
        </table>
    </div>
</main>

Controller

根据id,查询用户信息
查询所有部门信息,返回修改页面

//来到修改页面,查出当前员工,在页面回显
@GetMapping("/emp/{id}")
public String toEditPage(@PathVariable("id") Integer id, Model model) {
    Employee employee = employeeDao.get(id);
    model.addAttribute("emp", employee);

    //页面要显示所有的部门列表
    Collection<Department> departments = departmentDao.getDepartments();
    model.addAttribute("depts", departments);
    //回到修改页面(add是一个修改添加二合一的页面);
    return "emp/add";
}

回显用户信息

<input name="lastName" type="text" class="form-control" placeholder="zhangsan" th:value="${emp!=null}?${emp.lastName}">

回显选中部门
如果,当前部门id,等于用户的部门id

th:selected

设置为选中

<!--提交的是部门的id-->
<select class="form-control" name="department.id">
    <option th:selected="${emp!=null}?${dept.id == emp.department.id}" th:value="${dept.id}" th:each="dept:${depts}" th:text="${dept.departmentName}">1</option>
</select>

出生日期

#dates.format

格式化为指定日期格式

<input name="birth" type="text" class="form-control" placeholder="zhangsan" th:value="${emp!=null}?${#dates.format(emp.birth, 'yyyy-MM-dd HH:mm')}">

三元运算符
添加用户、修改用户共用一个页面

添加的时候,emp为空
修改的时候,emp不为空,显示用户信息

th:checked="${emp!=null}?${emp.gender==1}

提交按钮
添加的时候,显示添加
修改的时候,显示修改

<button type="submit" class="btn btn-primary" th:text="${emp!=null}?'修改':'添加'">添加</button>

点击添加
设置发送put请求

还是会执行form表单的action请求
提交方式,使用配置的put方式提交

<input type="hidden" name="_method" value="put" th:if="${emp!=null}"/>

Controller

保存员工信息

//员工修改;需要提交员工id;
@PutMapping("/emp")
public String updateEmployee(Employee employee) {
    System.out.println("修改的员工数据:" + employee);
    employeeDao.save(employee);
    return "redirect:/emps";
}

员工id
使用隐藏域

当添加时,emp为null,不生成input标签
用户的id,在后台自动的生成id

当修改emp不为null时,生成input标签
Name为id,value为修改用户id

<input type="hidden" name="id" th:if="${emp!=null}" th:value="${emp.id}">

公共页面

<main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4">
    <!--需要区分是员工修改还是添加;-->
    <form th:action="@{/emp}" method="post">
        <!--发送put请求修改员工数据-->
        <!--
        1、SpringMVC中配置HiddenHttpMethodFilter;(SpringBoot自动配置好的)
        2、页面创建一个post表单
        3、创建一个input项,name="_method";值就是我们指定的请求方式
        -->
        <input type="hidden" name="_method" value="put" th:if="${emp!=null}"/>
        <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:selected="${emp!=null}?${dept.id == emp.department.id}" th:value="${dept.id}" th:each="dept:${depts}" th:text="${dept.departmentName}">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>
</main>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值