[项目实战(一)]
[项目实战(二)]
[项目实践(三)]
[项目实践(四)]
[项目实践(五)]
[项目实践(六)]
[项目实践(八)]
[项目实践(九)]
一、在员工控制器(EmployeeController)里面新增“添加员工方法”
由于添加员工一是需要跳转到“添加页面”,二是提交“添加信息”并跳转到员工信息展示页面,相应的在员工控制器(EmployeeController)里面添加如下方法:
package com.xuyuan.springboot03web.controller;
import com.sun.org.apache.xpath.internal.operations.Mod;
import com.xuyuan.springboot03web.mapper.DepartmentMapper;
import com.xuyuan.springboot03web.mapper.EmployeeMapper;
import com.xuyuan.springboot03web.pojo.Department;
import com.xuyuan.springboot03web.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.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Collection;
@Controller
public class EmployeeController {
@Autowired
EmployeeMapper employeeMapper;
@Autowired
DepartmentMapper departmentMapper;
@RequestMapping("/emps")
public String list(Model model){
Collection<Employee> employees = employeeMapper.getAllEmployees();
model.addAttribute("emps",employees);
return "emp/list";
}
@GetMapping("/emp")
public String add(Model model){
//查出所有部门的信息
Collection<Department> allDepartments = departmentMapper.getAllDepartments();
model.addAttribute("allDepartments",allDepartments);
return "emp/add";
}
@PostMapping("/emp")
public String addEmp(Employee employee){
//添加的操作
employeeMapper.save(employee);//调用底层业务保存员工信息
return "redirect:/emps";
}
}
二、新建“添加用户页面”add.html
在emp目录下新建add.html文件
拷贝list页面,内容页可以在BOOTstrp中下载
<!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="@{/emp}" 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="dept:${allDepartments}"
th:text="${dept.getDepartmentName()}"
th:value="${dept.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="@{/emp}">添加员工</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:${emps}">
<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.yaml文件里添加如下语句
#默认日期格式
spring.mvc.date-format=yyyy-MM-dd
默认yyyy/MM=dd-ss-mm-ss
便可将日期改为一般格式。
启动项目后,进入员工管理页面,点击新增员工按钮,进入新增页面。
五、总结
增加员工实现需要注意的地方有:
需要新建一个添加员工页面。
由于未使用数据库,因此新增的信息不具有持久性,再次启动项目,新增信息便会消失。
理论上新增员工不能和之前的员工重复,而这里并未作判断。