这篇笔记的内容是实现一个添加员工,然后返回到员工列表主页的功能。
首先点击【添加员工】的按钮:
进入到添加员工的页面后,填写新员工的信息,然后点击【添加】按钮:
添加完员工后返回员工列表主页,同时可以看到新添加的员工已经在员工列表中:
新增添加员工的页面
在如图所示的目录位置添加一个add.html页面:
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 href="asserts/css/bootstrap.min.css" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="asserts/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/bar::topbar}"></div>
<div class="container-fluid">
<div class="row">
<div th:replace="~{commons/bar::sidebar(activeUri='emps')}"></div>
<main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4">
<form th:action="@{/add_staff}" method="post">
<div class="form-group">
<label>LastName</label>
<input type="text" name="lastName" class="form-control" placeholder="zhangsan">
</div>
<div class="form-group">
<label>Email</label>
<input type="email" name="email" class="form-control" placeholder="zhangsan@atguigu.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" name="department.id">
<option th:value="${dept.id}" th:each="dept:${depts}" th:text="${dept.departmentName}">1</option>
</select>
</div>
<div class="form-group">
<label>Birth</label>
<input type="text" name="birth" class="form-control" placeholder="2000/1/1">
</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>
这两句代码引入了顶栏和侧边栏的公共组件:
<div th:replace="~{commons/bar::topbar}"></div>
<div th:replace="~{commons/bar::sidebar(activeUri='emps')}"></div>
每个<input>模块中都有一个"name"来标识当前输入内容的名称,通过这种方式可以将html页面中的数据传递到控制器,然后在控制器中完成添加员工以及将员工信息入库的操作。
<form>表单的代码为:
<form th:action="@{/add_staff}" method="post">
表示表单数据将传递到"add_staff"的按钮事件。
对应的控制器为:
// 按钮事件,点击按钮表示添加员工完成,返回员工列表页面
@PostMapping("add_staff")
public String addEmp(Employee employee){
employeeDao.save(employee);
System.out.println("员工信息"+employee.toString());
return "redirect:/show_all_emps";
}
表单传递的数据会传递到这个控制器中。由于html文件的各个输入信息已经通过"name"标签直接传递到employee信息中,因此可以直接将新员工的信息导入到EmployeeDao中。
同时完成整个过程后重定向到localhost:8080/show_all_emps这个地址去,即显示所有的员工列表。