SpringBoot全网最详细学习笔记06

SpringBoot学习笔记06

员工管理系统

5. 展示员工列表

  1. 修改菜单名为员工管理

    image-20210518153604466

    修改成功:

    image-20210518153702044

  2. 编写员工管理的Controller, EmployeeController

    @Controller
    public class EmployeeController {
    
        @Autowired
        EmployeeDao employeeDao;
    
        @RequestMapping("/emps")
        public String list(Model model){
            Collection<Employee> employees = employeeDao.getAll();
    
            model.addAttribute("emps", employees);
    
            return "emp/list";
        }
    
    }
    

    将list.html移到新建的emp文件夹中<项目规范>

    image-20210518154528355

    在dashboard.html中修改以下内容, 实现跳转

    运行, 跳转成功.

    image-20210518155011750

  3. 抽取公共部分,实现复用

    在templates下新建一个commons下新建一个commons并在其下面新建一个commons.html

    将头部导航栏以及侧边栏代码写入

    image-20210518163309379

    修改部分:

    image-20210518163511525

    在dashboard.html以及list.html中引入

    image-20210518164015835

    运行, 复用成功.

  4. 高亮实现

    active属性代表高亮

    在dashboard.html以及list.html增加传递参数以识别是哪一个菜单高亮

    dashboard.html

    image-20210518170644075

    list.html

    image-20210518170711750

    修改commons.html内容

    员工管理:

    image-20210518170753093

    首页:

    image-20210518170926076

  5. 员工管理页面显示自己的数据

    修改list页面

    image-20210518172340878

    <table class="table table-striped table-sm">
    							<thead>
    								<tr>
    									<th>id</th>
    									<th>lastName</th>
    									<th>email</th>
    									<th>gender</th>
    									<th>department</th>
    									<th>birth</th>
    								</tr>
    							</thead>
    							<tbody>
    								<tr th:each="emp:${emps}">
    									<td th:text="${emp.getId()}"></td>
    									<td th:text="${emp.getLastName()}"></td>
    									<td th:text="${emp.getEmail()}"></td>
    									<td th:text="${emp.getGender()}"></td>
    									<td th:text="${emp.getDepartment().getDepartmentName()}"></td>
    									<td th:text="${emp.getBirth()}"></td>
    								</tr>
    							</tbody>
    						</table>
    

    运行,如下

    image-20210518172417124

  6. 增加功能按键

    list.html增加下列代码

    image-20210518172615527

    运行, 成功

    image-20210518172745966

  7. 男女&生日列显示问题

    修改list.html下列内容:

    image-20210518173103503

    运行, 解决成功

    image-20210518173137218

6. 添加员工实现

  1. 增加添加按钮

    在list.html中添加如下内容

    image-20210519223535895

  2. 在EmployeeController中添加下列内容

    image-20210519224018360

        @GetMapping("/emp")
        public String toAddpage(){
            return "emp/add";
        }
    
    
  3. 在emp下编写add.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/commons::topbar}"></div>
    
    <div class="container-fluid">
        <div class="row">
            <!--侧边栏-->
            <div th:replace="~{commons/commons::sidebar(active='list.html')}"></div>
    
            <main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4">
                <form action="">
                    <div class="form-group">
                        <label>LastName</label>
                        <input type="text"class="form-control"placeholder="祝怡睿">
                    </div>
                    <div class="form-group">
                        <label>Email</label>
                        <input type="email"class="form-control"placeholder="3289913719@qq.com">
                    </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">
                        <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">
                            <label class="form-check-label"></label>
                        </div>
                    </div><div class="form-group">
                    <label>department</label>
                    <select class="form-control">
                        <option>1</option>
                        <option>2</option>
                        <option>3</option>
                        <option>4</option>
                        <option>5</option>
                    </select>
                </div><div class="form-group">
                    <label>Birth</label>
                    <input type="text"class="form-control"placeholder="2000-03-28">
                </div>
                    <button type="submit"class="btn btn-primary">添加</button>
                </form>
            </main>
        </div>
    </div>
    
    <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script type="text/javascript" src="asserts/js/jquery-3.2.1.slim.min.js"></script>
    <script type="text/javascript" src="asserts/js/popper.min.js"></script>
    <script type="text/javascript" src="asserts/js/bootstrap.min.js"></script>
    
    <!-- Icons -->
    <script type="text/javascript" src="asserts/js/feather.min.js"></script>
    <script>
        feather.replace()
    </script>
    
    <!-- Graphs -->
    <script type="text/javascript" src="asserts/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>
    
  4. 编写完成运行, 如下

    image-20210519231650509

    点击添加:

    image-20210519231708317

  5. 将add.html 的name标签与Employee类对应

    image-20210519232244817

    修改后的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/commons::topbar}"></div>
    
    <div class="container-fluid">
        <div class="row">
            <!--侧边栏-->
            <div th:replace="~{commons/commons::sidebar(active='list.html')}"></div>
    
            <main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4">
                <form action="">
                    <div class="form-group">
                        <label>LastName</label>
                        <input type="text"class="form-control"placeholder="祝怡睿" name="lastName">
                    </div>
                    <div class="form-group">
                        <label>Email</label>
                        <input type="email"class="form-control"placeholder="3289913719@qq.com" name="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">
                        <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">
                            <label class="form-check-label"></label>
                        </div>
                    </div>
                    <div class="form-group">
                    <label>department</label>
                    <select class="form-control" name="department.id">
                        <option>1</option>
                        <option>2</option>
                        <option>3</option>
                        <option>4</option>
                        <option>5</option>
                    </select>
                </div>
                    <div class="form-group">
                    <label>Birth</label>
                    <input type="text"class="form-control"placeholder="2000-03-28" name="birth">
                    </div>
                    <button type="submit"class="btn btn-primary">添加</button>
                </form>
            </main>
        </div>
    </div>
    
    <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script type="text/javascript" src="asserts/js/jquery-3.2.1.slim.min.js"></script>
    <script type="text/javascript" src="asserts/js/popper.min.js"></script>
    <script type="text/javascript" src="asserts/js/bootstrap.min.js"></script>
    
    <!-- Icons -->
    <script type="text/javascript" src="asserts/js/feather.min.js"></script>
    <script>
        feather.replace()
    </script>
    
    <!-- Graphs -->
    <script type="text/javascript" src="asserts/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>
    

    运行发现部门框全部为数字,需要修改:

    image-20210519232517621

    需要从后端获取部门, 修改EmployeeController

    image-20210519232807109

    代码如下:

    package com.zyr.controller;
    
    import com.zyr.dao.DepartmentDao;
    import com.zyr.dao.EmployeeDao;
    import com.zyr.pojo.Department;
    import com.zyr.pojo.Employee;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import java.util.Collection;
    
    /**
     * @Author: ZYR
     * @Date: 2021/5/18 15:38
     * @Version 1.0
     */
    
    @Controller
    public class EmployeeController {
    
        @Autowired
        EmployeeDao employeeDao;
    
        @Autowired
        DepartmentDao departmentDao;
    
        @RequestMapping("/emps")
        public String list(Model model){
            Collection<Employee> employees = employeeDao.getAll();
    
            model.addAttribute("emps", employees);
    
            return "emp/list";
        }
    
        @GetMapping("/emp")
        public String toAddpage(Model model){
            //查处部门所有信息
            Collection<Department> departments = departmentDao.getDepartments();
            model.addAttribute("departments", departments);
            return "emp/add";
        }
    
    }
    

    对应前端需要接收departments信息

    image-20210519233246227

    运行结果如下:

    image-20210519233341199

  6. 添加结果提交

    • 修改add.html的form表单提交跳转位置

      image-20210519233804621

    • 在EmployeeController里添加功能实现内容

          @PostMapping("/emp")
          public String addEmp(Employee employee){
              //添加操作
              employeeDao.save(employee);
              System.out.println("save"+employee);
      
              return "redirect:/emps";
          }
      
    • 运行, 添加成功

      image-20210520000641976

  7. 默认时间格式修改

    在application.yaml中修改

    spring:
      mvc:
        date-format: yyyy-MM-dd
    

7. 修改员工实现

  1. 在list.html的编辑按钮添加如下内容

    image-20210520015705167

    代码如下:

    <a class="btn btn-sm btn-primary" th:href="@{/emp/{id}(id = ${emp.getId()})}">编辑</a>
    
  2. EmployeeController跳转实现

        //去员工修改页面
        @GetMapping("/emp/{id}")
        public String toUpdateEmp(@PathVariable("id") Integer id, Model model){
            //查出原来数据
            Employee employeeById = employeeDao.getEmployeeById(id);
            //将员工信息传给前端
            model.addAttribute("emp", employeeById);
            //查出部门所有信息
            Collection<Department> departments = departmentDao.getDepartments();
            model.addAttribute("departments", departments);
    
            return "emp/update";
        }
    
  3. 前端将选中的员工信息回显到修改页update.html

    image-20210520022741622

    具体代码:

    <!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/commons::topbar}"></div>
    
    <div class="container-fluid">
        <div class="row">
            <!--侧边栏-->
            <div th:replace="~{commons/commons::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="@{/updateEmp}" method="post">
                    <div class="form-group">
                        <label>LastName</label>
                        <input th:value="${emp.getLastName()}" type="text"class="form-control"placeholder="祝怡睿" name="lastName">
                    </div>
                    <div class="form-group">
                        <label>Email</label>
                        <input th:value="${emp.getEmail()}" type="email"class="form-control"placeholder="3289913719@qq.com" name="email">
                    </div>
                    <div class="form-group">
                    <label>Gender</label>
                    <br>
                    <div class="form-check form-check-inline">
                        <input th:checked="${emp.getGender()==1}" class="form-check-input" type="radio" name="gender" value="1">
                        <label class="form-check-label"></label>
                    </div>
                        <div class="form-check form-check-inline">
                            <input th:checked="${emp.getGender()==0}" class="form-check-input" type="radio" name="gender" value="0">
                            <label class="form-check-label"></label>
                        </div>
                    </div>
                    <div class="form-group">
                    <label>department</label>
                    <select class="form-control" name="department.id">
                        <option th:selected="${dept.getId()==emp.getDepartment().getId()}" th:each="dept:${departments}" th:text="${dept.getDepartmentName()}" th:value="${dept.getId()}"></option>
                    </select>
                </div>
                    <div class="form-group">
                    <label>Birth</label>
                    <input th:value="${#dates.format(emp.getBirth(), 'yyyy-MM-dd HH:mm')}" type="text"class="form-control"placeholder="2000-03-28" name="birth">
                    </div>
                    <button type="submit"class="btn btn-primary">修改</button>
                </form>
            </main>
        </div>
    </div>
    
    <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script type="text/javascript" src="asserts/js/jquery-3.2.1.slim.min.js"></script>
    <script type="text/javascript" src="asserts/js/popper.min.js"></script>
    <script type="text/javascript" src="asserts/js/bootstrap.min.js"></script>
    
    <!-- Icons -->
    <script type="text/javascript" src="asserts/js/feather.min.js"></script>
    <script>
        feather.replace()
    </script>
    
    <!-- Graphs -->
    <script type="text/javascript" src="asserts/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>
    
  4. 在EmployeeController添加修改功能

    //修改员工信息
        @PostMapping("/updateEmp")
        public String updateEmp(Employee employee){
    
            employeeDao.save(employee);
    
            return "redirect:/emps";
        }
    
  5. 日期问题

    image-20210520023155600

  6. 运行,修改

    image-20210520023409293

    发现修改后只是新增了一条

  7. 解决, 使用隐藏域解决

    update.html添加如下内容

    image-20210520023702033

    再次修改<将B性别修改为女>, 成功.

    image-20210520024120906

8. 删除员工实现

  1. 修改list.html删除按钮内容

    image-20210520193820309

    代码:

    <a class="btn btn-sm btn-danger" th:href="@{/deleemp/{id}(id = ${emp.getId()})}">删除</a>
    
  2. 添加EmployeeController删除功能

        //删除员工信息
        @GetMapping("/deleemp/{id}")
        public String deleEmp(@PathVariable("id") Integer id){
            employeeDao.delete(id);
            return "redirect:/emps";
        }
    
  3. 测试删除功能, 删除1005, 成功

    image-20210520195007890

9. 其他问题

  1. 404页面

    SpringBoot中404页面只需要在templates下新建一个error文件夹,然后其中建一个404.html,就生效了。

    image-20210520195503187

    其他错误页面方法一致(例如500等),运行测试

    image-20210520195546019

  2. 账号注销

    • 修改commons.html相关内容

      image-20210520195816733

      <a class="nav-link" th:href="@{/user/logout}">注销</a>
      
    • 在LoginController中添加相关内容

      @RequestMapping("/user/logout")
          public String logout(HttpSession session){
              session.invalidate();
              return "redirect:/index";
          }
      
    • 运行测试

10. 如何写一个网站

  1. 前端搞定:页面长什么样: 数据

  2. 设计数据库 (难点)

  3. 前段让他能独立运行,独立化工程

  4. 数据接口如何对接:json, 对象all in one!
    s";
    }

    
    
  5. 测试删除功能, 删除1005, 成功

    [外链图片转存中…(img-2qfFnTHY-1621856890171)]

9. 其他问题

  1. 404页面

    SpringBoot中404页面只需要在templates下新建一个error文件夹,然后其中建一个404.html,就生效了。

    [外链图片转存中…(img-vo6wSsty-1621856890171)]

    其他错误页面方法一致(例如500等),运行测试

    [外链图片转存中…(img-3T8bMpEM-1621856890172)]

  2. 账号注销

    • 修改commons.html相关内容

      [外链图片转存中…(img-iWgpau5o-1621856890174)]

      <a class="nav-link" th:href="@{/user/logout}">注销</a>
      
    • 在LoginController中添加相关内容

      @RequestMapping("/user/logout")
          public String logout(HttpSession session){
              session.invalidate();
              return "redirect:/index";
          }
      
    • 运行测试

10. 如何写一个网站

  1. 前端搞定:页面长什么样: 数据
  2. 设计数据库 (难点)
  3. 前段让他能独立运行,独立化工程
  4. 数据接口如何对接:json, 对象all in one!
  5. 前后端联调测试!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值