```
`list.html`页面员工展示:
```html
| # |
lastName |
email |
gender |
department |
birth |
操作 |
|
[[${emp.lastName}]] |
|
| |
|
|
```
效果:

#### (6)、CRUD员工添加
`list`页面添加按钮:
```html
```
`controlloer`:
```java
//来到员工添加页面
@GetMapping("/emp")
public String toAddPage(Model model){
//来到添加页面, 需要所有的部门,在页面显示
Collection
departments = departmentDao.getDepartments();
model.addAttribute("depts", departments);
return "emp/add";
}
```
`add.html`页面中的表单添加页面
```html
```
提交的数据格式不对:生日:日期;
2017-12-12;2017/12/12;2017.12.12;
配置(`application.properties`中配置)
```properties
# 日期格式化
spring.mvc.date-format=yyyy-MM-dd
```
日期的格式化;SpringMVC将页面提交的值需要转换为指定的类型;
2017-12-12---Date; 类型转换,格式化;
**默认日期是按照/的方式;**
#### (7)、CRUD员工修改
**修改添加二合一表单**
```html
```
后台`java`代码:
来到修改页面代码:
```javascript
//来到修改页面,查出当前员工,在页面回显
@GetMapping("/emp/{id}")
public String toEditPage(@PathVariable("id") Integer id, Model model){
Employee employee = employeeDao.get(id);
model.addAttribute("emp",employee);
//页面要显示所有的部门列表
Collection departments = departmentDao.getDepartments();
model.addAttribute("depts",departments);
//回到修改页面(add是一个修改添加二合一的页面);
return "emp/add";
}
```
真正修改:
```java
//员工修改;需要提交员工id;
@PutMapping("/emp")
public String updateEmployee(Employee employee){
System.out.println("修改的员工数据:"+employee);
employeeDao.save(employee);
return "redirect:/emps";
}
```
#### (8)、CRUD员工删除
```html
|
[[${emp.lastName}]] |
|
|
|
|
编辑
|
```
后台java代码:
```java
//员工删除
@DeleteMapping("/emp/{id}")
public String deleteEmployee(@PathVariable("id") Integer id){
employeeDao.delete(id);
return "redirect:/emps";
}
```
### 7、错误处理机制
#### (1)、SpringBoot默认的错误处理机制
默认效果,浏览器返回一个默认的错误页面

浏览器发送请求的请求头():
如果是其他客户端,默认响应一个`json`数据(Postman客户端查看):
```json
{
"timestamp": "2019-02-10T05:04:15.852+0000",
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/crud/aaa"
}
```
原理:
可以参照`ErrorMvcAutoConfiguration`;错误处理的自动配置;
给容器中添加了以下组件:
`1、DefaultErrorAttributes`:
```java
帮我们在页面共享信息;页面能获取的信息getErrorAttributes
@Override
public Map getErrorAttributes(RequestAttributes requestAttributes,
boolean includeStackTrace) {
Map errorAttributes = new LinkedHashMap();
errorAttributes.put("timestamp", new Date());
addStatus(errorAttributes, requestAttributes);
addErrorDetails(errorAttributes, requestAttributes, includeStackTrace);
addPath(errorAttributes, requestAttributes);
return errorAttributes;
}
```
`2、BasicErrorController`:处理默认`/error`请求(可以在配置文件中配置)
这个就是上面那两种的`html`错误页面和`json`的效果。
```java
@Controller
@RequestMapping("${server.error.path:${error.path:/error}}")
public class BasicErrorController extends AbstractErrorController {
@RequestMapping(produces = "text/html")//产生html类型的数据;浏览器发送的请求来到这个方法处理
public ModelAndView errorHtml(HttpServletRequest request,
HttpServletResponse response) {
HttpStatus status = getStatus(request);
Map model = Collections.unmodifiableMap(getErrorAttributes(
request, isIncludeStackTrace(request, MediaType.TEXT_HTML)));
response.setStatus(status.value());
//去哪个页面作为错误页面;包含页面地址和页面内容 resolveErrorView方法
ModelAndView modelAndView = resolveErrorView(request, response, status, model);
return (modelAndView == null ? new ModelAndView("error", model) : modelAndView);
}
@RequestMapping
@ResponseBody //产生json数据,其他客户端来到这个方法处理;
public ResponseEntity