本次实战项目主要是借鉴b站上的视频资源【狂神说Java】SpringBoot最新教程IDEA版通俗易懂完成的,有需求的话,可以直接去b站观看完整的视频教程,本文若有不对之处,望不吝赐教,谢谢~
博文前提:
- Spring Boot项目实践-员工管理系统(一)·准备工作
- Spring Boot项目实践-员工管理系统(二)·首页实现
- Spring Boot项目实践-员工管理系统(三)·国际化
- Spring Boot项目实践-员工管理系统(四)·登录功能实现
- Spring Boot项目实践-员工管理系统(五)·登录拦截器
- Spring Boot项目实践-员工管理系统(六)·展示员工列表
- Spring Boot项目实践-员工管理系统(七)·增加员工实现
一、在员工控制器(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