[项目实战(一)]
[项目实战(二)]
[项目实践(三)]
[项目实践(四)]
[项目实践(五)]
[项目实践(六)]
[项目实践(七)]
[项目实践(九)]
一、在员工控制器(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.PathVariable;
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";
}
//去员工的修改页面
@GetMapping("/emp/{getEmployeeId}")
public String toupdataEMP( @PathVariable("getEmployeeId") Integer getEmployeeId ,Model model){
//查出原来的数据
Employee employee = employeeMapper.getEmployeeById(getEmployeeId);
model.addAttribute("emp",employee);
Collection<Department> allDepartments = departmentMapper.getAllDepartments();
model.addAttribute("allDepartments",allDepartments);
return "emp/updata";
}
}
二、新建修改员工信息页面(update.html)并且在list页面中修改跳转标签
list.html
<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="emp:${emps}">
<td th:text="${emp.getEmployeeId()}"></td>
<td th:text="${emp.getEmployeeName()}"></td>
<td th:text="${emp.getEmail()}"></td>
<td th:text="${emp.getGender()==0?'女':'男'}"></td>
<td th:text="${emp.department.getDepartmentName()}"></td>
<!--格式-->
<td th:text="${#dates.format(emp.getBirth(),'yyyy-MM-dd HH:mm:ss')}"></td>
<td>
<!-- -->
<a class="btn btn-sm btn-primary" th:href="@{/emp/}+${emp.getEmployeeId()}">编辑</a>
<button class="btn btn-sm btn-danger">删除</button>
</td>
</tr>
</tbody>
</table>
</div>
</main>
在emp下面创建updata页面 copy add页面
updata页面:
<!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">
<!--隐藏域问题-->
<input type="hidden" name="employeeId" th:value="${emp.getEmployeeId()}">
<div class="form-group">
<label>employeeName</label>
<label>
<input th:value="${emp.getEmployeeId()}" type="text" name="employeeName" class="form-control" placeholder="a">
</label>
</div>
<div class="form-group">
<label>email</label>
<label>
<input type="text" th:value="${emp.getEmail()}" 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" th:checked="${emp.getGender()==1}" 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" th:checked="${emp.getGender()==0}"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:selected="${dept.getDepartmentId()==emp.getDepartment().getDepartmentId()}" 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" th:value="${#dates.format(emp.getBirth(),'yyyy/MM/dd')}" 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>
三、启动项目
启动项目后,进入员工管理页面,点击要修改员工的“编辑”按钮。
输入相应的修改信息后点击修改