前言
采用Restful风格进行增删查改(CRUD)
一、映射规则
URI: /资源名称/资源标识 HTTP请求方式区分对资源CRUD操作
普通CRUD | RestfulCRUD | |
---|---|---|
查询 | getEmp | emp- - -GET |
添加 | addEmp?xxx | emp- - -POST |
修改 | updateEmp?id=xxx&xx=xxx | emp/{id}- - -PUT |
删除 | deleteEmp?id=xxx | emp/{id}- - -DELETE |
依据上面的原则,我们实验的请求架构如下:
实验功能 | 请求URI | 请求方式 |
---|---|---|
查询所有员工 | emps | GET |
查询某个员工(来到修改页面) | emp/1 | GET |
来到添加页面 | emp | GET |
添加员工 | emp | POST |
修改员工 | emp | PUT |
删除员工 | emp/1 | DELETE |
二、查询所有员工
1.发送请求th:href="@{/emps}"
<li class="nav-item">
<a class="nav-link"
href="#" th:href="@{/emps}">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-users">
<path d="M17 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2"></path>
<circle cx="9" cy="7" r="4"></circle>
<path d="M23 21v-2a4 4 0 0 0-3-3.87"></path>
<path d="M16 3.13a4 4 0 0 1 0 7.75"></path>
</svg>
员工管理
</a>
</li>
2.处理请求
//查询所有员工
@GetMapping("/emps")
public String list(Model model){
Collection<Employee> employees = employeeDao.getAll();
model.addAttribute("emps",employees);
//模板引擎默认的解析器
return "emp/list";
}
3.显示页面
<table class="table table-striped table-sm">
<thead>
<tr>
<th>#</th>
<th>lastName</th>
<th>email</th>
<th>gender</th>
<th>department</th>
<th>birth</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr th:each="emp:${emps}">
<td th:text=