SpringBoot学习笔记06
员工管理系统
5. 展示员工列表
-
修改菜单名为员工管理
修改成功:
-
编写员工管理的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文件夹中<项目规范>
在dashboard.html中修改以下内容, 实现跳转
运行, 跳转成功.
-
抽取公共部分,实现复用
在templates下新建一个commons下新建一个commons并在其下面新建一个commons.html
将头部导航栏以及侧边栏代码写入
修改部分:
在dashboard.html以及list.html中引入
运行, 复用成功.
-
高亮实现
active
属性代表高亮在dashboard.html以及list.html增加传递参数以识别是哪一个菜单高亮
dashboard.html
list.html
修改commons.html内容
员工管理:
首页:
-
员工管理页面显示自己的数据
修改list页面
<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>
运行,如下
-
增加功能按键
list.html增加下列代码
运行, 成功
-
男女&生日列显示问题
修改list.html下列内容:
运行, 解决成功
6. 添加员工实现
-
增加添加按钮
在list.html中添加如下内容
-
在EmployeeController中添加下列内容
@GetMapping("/emp") public String toAddpage(){ return "emp/add"; }
-
在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>
-
编写完成运行, 如下
点击添加:
-
将add.html 的name标签与Employee类对应
修改后的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>
运行发现部门框全部为数字,需要修改:
需要从后端获取部门, 修改EmployeeController
代码如下:
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信息
运行结果如下:
-
添加结果提交
-
修改add.html的form表单提交跳转位置
-
在EmployeeController里添加功能实现内容
@PostMapping("/emp") public String addEmp(Employee employee){ //添加操作 employeeDao.save(employee); System.out.println("save"+employee); return "redirect:/emps"; }
-
运行, 添加成功
-
-
默认时间格式修改
在application.yaml中修改
spring: mvc: date-format: yyyy-MM-dd
7. 修改员工实现
-
在list.html的编辑按钮添加如下内容
代码如下:
<a class="btn btn-sm btn-primary" th:href="@{/emp/{id}(id = ${emp.getId()})}">编辑</a>
-
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"; }
-
前端将选中的员工信息回显到修改页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/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>
-
在EmployeeController添加修改功能
//修改员工信息 @PostMapping("/updateEmp") public String updateEmp(Employee employee){ employeeDao.save(employee); return "redirect:/emps"; }
-
日期问题
-
运行,修改
发现修改后只是新增了一条
-
解决, 使用隐藏域解决
update.html添加如下内容
再次修改<将B性别修改为女>, 成功.
8. 删除员工实现
-
修改list.html删除按钮内容
代码:
<a class="btn btn-sm btn-danger" th:href="@{/deleemp/{id}(id = ${emp.getId()})}">删除</a>
-
添加EmployeeController删除功能
//删除员工信息 @GetMapping("/deleemp/{id}") public String deleEmp(@PathVariable("id") Integer id){ employeeDao.delete(id); return "redirect:/emps"; }
-
测试删除功能, 删除1005, 成功
9. 其他问题
-
404页面
SpringBoot中404页面只需要在templates下新建一个error文件夹,然后其中建一个404.html,就生效了。
其他错误页面方法一致(例如500等),运行测试
-
账号注销
-
修改commons.html相关内容
<a class="nav-link" th:href="@{/user/logout}">注销</a>
-
在LoginController中添加相关内容
@RequestMapping("/user/logout") public String logout(HttpSession session){ session.invalidate(); return "redirect:/index"; }
-
运行测试
-
10. 如何写一个网站
-
前端搞定:页面长什么样: 数据
-
设计数据库 (难点)
-
前段让他能独立运行,独立化工程
-
数据接口如何对接:json, 对象all in one!
s";
} -
测试删除功能, 删除1005, 成功
[外链图片转存中…(img-2qfFnTHY-1621856890171)]
9. 其他问题
-
404页面
SpringBoot中404页面只需要在templates下新建一个error文件夹,然后其中建一个404.html,就生效了。
[外链图片转存中…(img-vo6wSsty-1621856890171)]
其他错误页面方法一致(例如500等),运行测试
[外链图片转存中…(img-3T8bMpEM-1621856890172)]
-
账号注销
-
修改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. 如何写一个网站
- 前端搞定:页面长什么样: 数据
- 设计数据库 (难点)
- 前段让他能独立运行,独立化工程
- 数据接口如何对接:json, 对象all in one!
- 前后端联调测试!