Employee类:
public class Employee {
private Integer id;
private String lastName;
private String email;
//1 male, 0 female
private Integer gender;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public Employee(Integer id, String lastName, String email, Integer
gender) {
super();
this.id = id;
this.lastName = lastName;
this.email = email;
this.gender = gender;
}
public Employee() {
}
}
EmployeeDao类:
@Repository
public class EmployeeDao {
private static Map<Integer, Employee> employees = null;
static{
employees = new HashMap<Integer, Employee>();
employees.put(1001, new Employee(1001, "E-AA", "aa@163.com", 1));
employees.put(1002, new Employee(1002, "E-BB", "bb@163.com", 1));
employees.put(1003, new Employee(1003, "E-CC", "cc@163.com", 0));
employees.put(1004, new Employee(1004, "E-DD", "dd@163.com", 0));
employees.put(1005, new Employee(1005, "E-EE", "ee@163.com", 1));
}
private static Integer initId = 1006;
public void save(Employee employee){
if(employee.getId() == null){
employee.setId(initId++);
}
employees.put(employee.getId(), employee);
}
public Collection<Employee> getAll(){
return employees.values();
}
public Employee get(Integer id){
return employees.get(id);
}
public void delete(Integer id){
employees.remove(id);
}
}
IDEA设置自动导包:
功能 | URL 地址 | 请求方式 |
---|---|---|
访问首页√ | / | GET |
查询全部数据√ | /employee | GET |
删除√ | /employee/2 | DELETE |
跳转到添加数据页面√ | /toAdd | GET |
执行保存√ | /employee | POST |
跳转到更新数据页面√ | /employee/2 | GET |
执行更新√ | /employee | PUT |
<mvc:view-controller path="/" view-name="index"/>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" >
<title>Title</title>
</head>
<body>
<h1>首页</h1>
<a th:href="@{/employee}">访问员工信息</a>
</body>
</html>
@Controller
public class EmployeeController {
@Autowired
private EmployeeDao employeeDao;
@GetMapping("/employee")
public String getAllEmployee(Model model){
//获取所有的员工信息
Collection<Employee> all = employeeDao.getAll();
//将所有员工信息在请求域中共享
model.addAttribute("allEmployee",all);
return "employee_list";
}
}
扫描不到,需要更改:
<context:component-scan base-package="com.gao"/>
@GetMapping("/employee")
public String getAllEmployee(Model model){
//获取所有的员工信息
Collection<Employee> all = employeeDao.getAll();
//将所有员工信息在请求域中共享
model.addAttribute("allEmployee",all);
return "employee_list";
}
新建html(employee_list):
<table>
<tr>
<th colspan="5">employee_listth>
tr>
<tr>
<th>idth>
<th>lastNameth>
<th>emailth>
<th>genderth>
<th>optionsth>
tr>
<tr th:each="employee : ${allEmployee}">
<td th:text="${employee.id}">td>
<td th:text="${employee.lastName}">td>
<td th:text="${employee.email}">td>
<td th:text="${employee.gender}">td>
<td>
<a href="">updatea>
<a href="">deletea>
td>
tr>
table>
static静态资源:
引入资源:
<head>
<meta charset="UTF-8">
<title>employee_listtitle>
<link rel="stylesheet" th:href="@{/static/css/index.css}">
head>
配置
<mvc:default-servlet-handler/>
使用Vue.js
通过vue处理点击事件
<div id="app">
<table>
<tr>
<th colspan="5">employee_listth>
tr>
<tr>
<th>idth>
<th>lastNameth>
<th>emailth>
<th>genderth>
<th>options(<a th:href="@{/to/add}">adda>)th>
tr>
<tr th:each="employee : ${allEmployee}">
<td th:text="${employee.id}">td>
<td th:text="${employee.lastName}">td>
<td th:text="${employee.email}">td>
<td th:text="${employee.gender}">td>
<td>
<a @click="deleteEmployee"
th:href="@{'/employee/' + ${employee.id}}">deletea>
<a th:href="@{'/employee/' + ${employee.id}}">updatea>
td>
tr>
table>
<form id="delete_form" method="post">
<input type="hidden" name="_method" value="delete">
form>
div>
<script type="text/javascript" th:src="@{/static/js/vue.js}">script>
<script type="text/javascript">
var vue = new Vue({
el:"#app",
methods:{
//event表示当前事件
deleteEmployee:function (event) {
//通过id获取表单标签
var delete_form = document.getElementById("delete_form");
//将触发事件的超链接的href属性为表单的action属性赋值
delete_form.action = event.target.href;
//提交表单
delete_form.submit();
//阻止超链接的默认跳转行为
event.preventDefault();
}
}
});
script>
@DeleteMapping("/employee/{id}")
public String deleteEmployee(@PathVariable("id") Integer id){
//删除员工信息
employeeDao.delete(id);
//重定向到列表
return "redirect:/employee";
}
实现跳转到添加数据页面:
<th>options(<a th:href="@{/to/add}">adda>)th>
springmvc.xml配置:
<mvc:view-controller path="/" view-name="index">mvc:view-controller>
<mvc:view-controller path="/to/add" view-name="employee_add">mvc:view-controller>
employee_add.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>employee_add</title>
</head>
<body>
<form th:action="@{/employee}" method="post">
lastName:<input type="text" name="lastName"><br>
email:<input type="text" name="email"><br>
gender:<input type="radio" name="gender" value="1">male
<input type="radio" name="gender" value="0">female<br>
<input type="submit" value="add"><br>
</form>
</body>
</html>
重定向跳转:
@PostMapping("/employee")
public String addEmployee(Employee employee){
//保存员工信息
employeeDao.save(employee);
return "redirect:/employee";
}
update(注意格式
)
<a th:href="@{'/employee/' + ${employee.id}}">updatea>
页面跳转:
@GetMapping("/employee/{id}")
public String toUpdate(@PathVariable("id") Integer id,Model model){
//根据id查询员工信息
Employee employee = employeeDao.get(id);
//将员工信息共享到请求域中
model.addAttribute("employee",employee);
//跳转到employee_update.html
return "employee_update";
}
employee_update页面:
<form th:action="@{/employee}" method="post">
<input type="hidden" name="_method" value="put">
<input type="hidden" name="id" th:value="${employee.id}">
lastName:<input type="text" name="lastName" th:value="${employee.lastName}">
<br>
email:<input type="text" name="email" th:value="${employee.email}"><br>
gender:<input type="radio" name="gender" value="1"
th:field="${employee.gender}">male
<input type="radio" name="gender" value="0"
th:field="${employee.gender}">female<br>
<input type="submit" value="update"><br>
form>
update操作:
@PutMapping("/employee")
public String updateEmployee(Employee employee){
//修改员工信息
employeeDao.save(employee);
return "redirect:/employee";
}