本次实战项目主要是借鉴b站上的视频资源【狂神说Java】SpringBoot最新教程IDEA版通俗易懂完成的,有需求的话,可以直接去b站观看完整的视频教程,本文若有不对之处,望不吝赐教,谢谢~
博文前提:
- Spring Boot项目实践-员工管理系统(一)·准备工作
- Spring Boot项目实践-员工管理系统(二)·首页实现
- Spring Boot项目实践-员工管理系统(三)·国际化
- Spring Boot项目实践-员工管理系统(四)·登录功能实现
- Spring Boot项目实践-员工管理系统(五)·登录拦截器
- Spring Boot项目实践-员工管理系统(六)·展示员工列表
一、在员工控制器(EmployeeController)里面新增“添加员工方法”
由于添加员工一是需要跳转到“添加页面”,二是提交“添加信息”并跳转到员工信息展示页面,相应的在员工控制器(EmployeeController)里面添加如下方法:
@Autowired
DepartmentDao departmentDao;
/**
* 添加员工
* @return
*/
@GetMapping("/add")
public String add(Model model){
//获取所有部门信息
Collection<Department> departments=departmentDao.getAllDepartments();
model.addAttribute("departments",departments);
return "employees/add";
}
/**
* 提交添加员工信息
* @return
*/
@PostMapping("/submitadd")
public String submitAdd(Employee employee){
//保存用户
employeeDao.save(employee);
return "redirect:/employees";
}
二、新建“添加用户页面”add.html
在employees目录下新建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/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="@{/submitadd}" method="post">
<div class="form-group">
<label>employeeName</label>
<label>
<input type="text" name="employeeName" class="form-control" placeholder="a">
</label>
</div>
<div class="form-group">
<label>email</label>
<label>
<input type="text" name="email" class="form-control" placeholder="123@123.com">
</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">
</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">
</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()}"></option>
</select>
</label>
</div>
<div class="form-group">
<label>birth</label>
<label>
<input type="text" 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>
三、在员工展示页面(list.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">
<!--添加员工按钮-->
<h2>
<a class="btn btn-sm btn-success" th:href="@{/add}">添加员工</a>
</h2>
<div class="table-responsive">
<table class="table table-striped table-sm">
<thead>
<tr>
<th>id</th>
<th>username</th>
<th>email</th>
<th>gender</th>
<th>department</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr th:each="employee:${employees}">
<td th:text="${employee.getEmployeeId()}"></td>
<td th:text="${employee.getEmployeeName()}"></td>
<td th:text="${employee.getEmail()}"></td>
<td th:text="${employee.getGender()==0 ? '女':'男'}"></td>
<td th:text="${employee.department.getDepartmentName()}"></td>
<td th:text="${#dates.format(employee.getBirth(),'yyyy-MM-dd HH:mm:ss')}"></td>
<td>
<button class="btn btn-sm btn-primary">编辑</button>
<button class="btn btn-sm btn-danger">删除</button>
</td>
</tr>
</tbody>
</table>
</div>
</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>
四、修改默认时间格式并启动项目
按照习惯,书写时间的一般格式为yyyy-MM-dd,在application.properties文件里添加如下语句
#默认日期格式
spring.mvc.date-format=yyyy-MM-dd
便可将日期改为一般格式。
启动项目后,进入员工管理页面,点击新增员工按钮,进入新增页面。
输入相应的信息,并点击添加按钮。
添加成功。
五、总结
增加员工实现需要注意的地方有:
- 需要新建一个添加员工页面。
- 由于未使用数据库,因此新增的信息不具有持久性,再次启动项目,新增信息便会消失。
- 理论上新增员工不能和之前的员工重复,而这里并未作判断。
2020.04.13