Spring Boot項目實踐-員工管理系統(八)·修改員工信息

本次實戰項目主要是借鑑b站上的視頻資源【狂神說Java】SpringBoot最新教程IDEA版通俗易懂完成的,有需求的話,可以直接去b站觀看完整的視頻教程,本文若有不對之處,望不吝賜教,謝謝~
博文前提:

一、在員工控制器(EmployeeController)裏面新增“修改員工方法”

由於修改員工一是需要跳轉到“修改頁面”,二是提交“修改信息”並跳轉到員工信息展示頁面,相應的在員工控制器(EmployeeController)裏面添加如下方法:

/**
     * 修改員工信息
     * @param employeeId
     * @param model
     * @return
     */
    @GetMapping("/update/{employeeId}")
    public String update(@PathVariable("employeeId")Integer employeeId,Model model){

        //查找要修改員工的信息
        Employee employee=employeeDao.getEmployeeById(employeeId);
        model.addAttribute("employee",employee);

        //獲取所有部門信息
        Collection<Department> departments=departmentDao.getAllDepartments();
        model.addAttribute("departments",departments);

        return "employees/update";
    }


    /**
     * 提交修改員工信息
     * @param employee
     * @return
     */
    @PostMapping("/submitupdate")
    public String submitUpdate(Employee employee){
        employeeDao.save(employee);
        return "redirect:/employees";

    }

二、新建修改員工信息頁面(update.html)

在employees目錄下新建update.html文件

<!DOCTYPE html>
<!-- saved from url=(0052)http://getbootstrap.com/docs/4.0/examples/dashboard/ -->
<html lang="en" xmlns:th="http://www.thymeleaf.org">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>Dashboard Template for Bootstrap</title>
    <!-- Bootstrap core CSS -->
    <link th:href="@{/css/bootstrap.min.css}" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link th:href="@{/css/dashboard.css}" rel="stylesheet">
    <style type="text/css">
        /* Chart.js */

        @-webkit-keyframes chartjs-render-animation {
            from {
                opacity: 0.99
            }
            to {
                opacity: 1
            }
        }

        @keyframes chartjs-render-animation {
            from {
                opacity: 0.99
            }
            to {
                opacity: 1
            }
        }

        .chartjs-render-monitor {
            -webkit-animation: chartjs-render-animation 0.001s;
            animation: chartjs-render-animation 0.001s;
        }
    </style>
</head>
<body>

<!--頂部導航欄-->
<div th:replace="~{commons/common::topbar}"></div>

<div class="container-fluid">
    <div class="row">

        <!--側邊欄-->
        <div th:replace="~{commons/common::sidebar(active='list.html')}"></div>

        <main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4">

            <form th:action="@{/submitupdate}" method="post">

                <input type="hidden" name="employeeId" th:value="${employee.getEmployeeId()}">

                <div class="form-group">
                    <label>employeeName</label>
                    <label>
                        <input type="text" th:value="${employee.getEmployeeName()}" name="employeeName" class="form-control" placeholder="a">
                    </label>
                </div>

                <div class="form-group">
                    <label>email</label>
                    <label>
                        <input type="text" th:value="${employee.getEmail()}" name="email" class="form-control" placeholder="[email protected]">
                    </label>
                </div>

                <div class="form-group">
                    <label>gender</label>
                    <div class="form-check form-check-inline">
                        <label>
                            <input class="form-check-input" type="radio" name="gender" value="1"
                                   th:checked="${employee.getGender()==1}">
                        </label>
                        <label class="form-check-label"></label>
                    </div>
                    <div class="form-check form-check-inline">
                        <label>
                            <input class="form-check-input" type="radio" name="gender" value="0"
                                th:checked="${employee.getGender()==0}">
                        </label>
                        <label class="form-check-label"></label>
                    </div>
                </div>


                <div class="form-group">
                    <label>department</label>
                    <label>
                        <select class="form-control" name="department.departmentId">
                            <option th:each="department:${departments}"
                                    th:text="${department.getDepartmentName()}"
                                    th:value="${department.getDepartmentId()}"
                                    th:selected="${department.getDepartmentId()==employee.getDepartment().getDepartmentId()}"></option>

                        </select>
                    </label>
                </div>


                <div class="form-group">
                    <label>birth</label>
                    <label>
                        <input type="text" th:value="${#dates.format(employee.getBirth(),'yyyy-MM-dd HH:mm:ss')}" name="birth" class="form-control" placeholder="1990-01-01">
                    </label>
                </div>

                <div>
                    <button type="submit" class="btn btn-primary">修改</button>
                </div>
            </form>


        </main>

    </div>
</div>

<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script type="text/javascript" src="/js/jquery-3.2.1.slim.min.js"></script>
<script type="text/javascript" src="/js/popper.min.js"></script>
<script type="text/javascript" src="/js/bootstrap.min.js"></script>

<!-- Icons -->
<script type="text/javascript" src="/js/feather.min.js"></script>
<script>
    feather.replace()
</script>

<!-- Graphs -->
<script type="text/javascript" src="/js/Chart.min.js"></script>
<script>
    var ctx = document.getElementById("myChart");
    var myChart = new Chart(ctx, {
        type: 'line',
        data: {
            labels: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
            datasets: [{
                data: [15339, 21345, 18483, 24003, 23489, 24092, 12034],
                lineTension: 0,
                backgroundColor: 'transparent',
                borderColor: '#007bff',
                borderWidth: 4,
                pointBackgroundColor: '#007bff'
            }]
        },
        options: {
            scales: {
                yAxes: [{
                    ticks: {
                        beginAtZero: false
                    }
                }]
            },
            legend: {
                display: false,
            }
        }
    });
</script>

</body>

</html>

三、啓動項目

啓動項目後,進入員工管理頁面,點擊要修改員工的“編輯”按鈕。
在這裏插入圖片描述
輸入相應的修改信息後點擊提交。
在這裏插入圖片描述
修改成功。
在這裏插入圖片描述

在這裏插入圖片描述
2020.04.13

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章