package com.uncle.controller;
import com.uncle.bean.Guest;
import com.uncle.service.GuestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Controller
@RequestMapping("/guest")
public class GuestController {
@Autowired
private GuestService guestService;
@GetMapping
public String list(Model model) {
List<Guest> list = guestService.list();
model.addAttribute("list", list);
return "list";
}
@GetMapping("/toAdd")
public String toAdd() {
return "add";
}
@PostMapping
public String add(Guest guest) {
guestService.add(guest);
return "redirect:/guest";
}
@GetMapping("/toUpdate/{name}")
public String toUpdate(Model model,@PathVariable("name") String name) {
Guest guest = guestService.getName(name);
System.out.println(guest);
model.addAttribute("guest", guest);
return "update";
}
@PutMapping
public String update(Guest guest) {
guestService.update(guest);
return "redirect:/guest";
}
@DeleteMapping("{name}")
public String delete(@PathVariable("name") String name) {
guestService.delete(name);
return "redirect:/guest";
}
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>展示页面</title>
<link rel="stylesheet" th:href="@{/bootstrap.css}">
<script type="text/javascript" th:src="@{/webjars/jquery/3.6.0/jquery.js}"></script>
</head>
<body class="container">
<h3>中餐厅嘉宾如下</h3>
<br/>
<table class="table table-hover">
<thead>
<tr>
<th>名字</th>
<th>角色</th>
</tr>
</thead>
<tbody>
<tr th:each="guest:${list}">
<td th:text="${guest.name}">name</td>
<td th:text="${guest.role}">role</td>
<td class="col-sm-2">
<a th:href="@{/guest/toUpdate/}+${guest.name}">编辑</a>
</td>
<td class="col-sm-2">
<!-- <a th:href="@{/guest/delete(name=${guest.name})}">删除</a>-->
<button th:attr="del_url=@{/guest/}+${guest.name}" name="del_button">删除</button>
</td>
</tr>
</tbody>
</table>
<div class="form-group">
<div class="col-sm-2 control-label">
<a href="/guest/toAdd" th:href="@{/guest/toAdd}" class="btn btn-info">添加</a>
</div>
</div>
<!--删除按钮借助的表单-->
<form method="post" id="del_form">
<input type="hidden" name="_method" value="delete">
</form>
<script>
$(function () {
$("button[name='del_button']").click(function () {
$("#del_form").prop("action",$(this).attr("del_url")).submit();
});
});
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" th:href="@{/bootstrap.css}">
</head>
<body class="container">
<h3>
添加嘉宾
</h3>
<form th:action="@{/guest}" method="post" class="form-horizontal">
<div class="form-group">
<label class="col-sm-2 control-label">名字</label>
<div class="col-sm-5">
<input type="text" id="name" name="name">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">角色</label>
<div class="col-sm-5">
<input type="text" id="role" name="role">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-5">
<input type="submit" value="确认添加" class="btn btn-info">
</div>
</div>
</form>
</body>
</html>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>修改角色</title>
<link rel="stylesheet" th:href="@{/bootstrap.css}">
</head>
<body class="container">
<h3>
修改角色
</h3>
<form th:action="@{/guest}" th:object="${guest}" method="post" class="form-horizontal">
<input type="hidden" name="_method" value="put">
<div class="form-group">
<label class="col-sm-2 control-label" >名字</label>
<div class="col-sm-5">
<div class="col-sm-5">
<input type="text" id="name" name="name"
th:value="*{name}" class="form-control" readonly>
</div>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" >角色</label>
<div class="col-sm-5">
<input type="text" id="role" name="role"
th:value="*{role}" class="form-control">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-5">
<input type="submit" value="确认修改" class="btn btn-info">
</div>
</div>
</form>
</body>
</html>
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c2c0d64b9b69e4642a9ad8e0fdfe3879.png)