一、添加员工
添加员工无非就如下几个步骤:
1、添加员工按钮,在相应合适的地方加上按钮,具体页面就不展示了,只展示关键的部分
<h2><a class="btn btn-sm btn-success" th:href="@{/emp}">添加员工</a></h2>
2、跳转到添加页面–对应的controller
@RequestMapping("/emps")
//展示所有员工列表的操作
public String List(Model model){
Collection<Employee> allEmployee = employeeDao.getAllEmployee();
model.addAttribute("emps",allEmployee);
return "emp/list";
}
//restful风格
//跳转到添加员工页面
@GetMapping("/emp")
public String toAddPage(Model model){
//跳转的同时把获取到的所有的部门信息通过Model存放到session域中
Collection<Department> allDepartment = departmentDao.getAllDepartment();
model.addAttribute("allDepartment",allDepartment);
return "emp/add";
}
由于dao层用并没有用到数据库,而是用的静态数据经行模拟,这里也放上对应dao层的代码
@Repository
public class DepartmentDao {
//模拟数据库中的数据
private static Map<Integer,Department> departments = null;
static {
departments = new HashMap<Integer, Department>();
departments.put(101,new Department(101,"教学部"));
departments.put(102,new Department(102,"市场部"));
departments.put(103,new Department(103,"教研部"));
departments.put(104,new Department(104,"运营部"));
departments.put(105,new Department(105,"后勤部"));
}
//获取所有的部门
public Collection<Department> getAllDepartment(){
return departments.values();
}
//通过id获得部门,通过key获取value
public Department getDepartmentByID(Integer id){
return departments.get(id);
}
}
3、添加员工成功
前端相关代码:
<form th:action="@{/emp}" method="post">
//相关的表单元素在这里不一一展示了……从上一步跳转到添加员工页面,要注意的一点是表单的相关name属性切记一定要和pojo类的属性名一一对应才行
//不然的话无法通过springboot自己的属性封装机制拿到相应的值。
</form>
后端相关代码:
//restful风格
//添加员工操作
@PostMapping("/emp")
public String addEmp(Employee employee){
//添加操作
employeeDao.save(employee);
//直接跳转到controller第一个请求,重新显示所有员工
return "redirect:/emps";
}
至此添加操作完成~需要注意的是,如果像添加操作涉及到日期格式什么的,例如添加员工的生日,springboot默认的日期格式是以"/的格式写的,例如yyyy/MM/dd,如果我们像修改默认的日期格式,可以在application.properties文件中修改默认配置,只需要加入如下代码即可:
# 格式化日期
spring.mvc.format.date=yyyy-MM-dd
二、修改员工
1.按钮跳转到相关修改页面
通过"+"拼接字符串,用${}取值达到传参效果
前端:
//点击传参并跳转到相关页面
<a th:href="@{/emp/}+${emp.getId()}" class="btn btn-sm btn-primary">编辑</a>
后端Controller:
//跳转到修改员工页面
//形如xxx/{}的请求通过@PathVariable来接收参数
//形如xxx/xx?x=的请求通过@RequestParam来接收参数
@GetMapping("/emp/{id}")
public String toModifyPage(@PathVariable("id")Integer id, Model model){
//获得具体的员工
Employee employee = employeeDao.getEmployeeById(id);
model.addAttribute("emp",employee);
//获取所有部门,以方便修改员工所属的部门
Collection<Department> allDepartment = departmentDao.getAllDepartment();
model.addAttribute("allDepartment",allDepartment);
return "emp/modify";
}
2.数据回显
前端:
<form th:action="@{/update}" method="post">
<!--隐藏域id,因为id是主键自增的,防止每修改一次增加一条记录,确保都是当前id的操作-->
<input type="hidden" name="id" th:value="${emp.getId()}">
<div class="form-group">
<label>LastName</label>
//通过th:value回显数据,由于操作类似,这里仅展示其中不同的操作
<input th:value="${emp.getName()}" type="text" name="name" class="form-control" placeholder="海绵宝宝">
</div>
<div class="form-group">
<label>Birth</label>
//这里通过#datas,format对日期格式进行修改
<input th:value="${#dates.format(emp.getBirth(),'yyyy-MM-dd HH:mm')}" type="text" name="birth" class="form-control" placeholder="嘤嘤嘤">
</div>
</form>
3.点击保存修改成功
@PostMapping("/update")
public String update(Employee employee){
employeeDao.save(employee);
//重定向到emps查看所有员工
return "redirect:/emps";
}
三、删除员工
1.点击删除按钮
前端:
<a th:href="@{/emp/delete/}+${emp.getId()}" class="btn btn-sm btn-danger">删除</a>
2.删除成功
后端:
//删除员工操作
@GetMapping("/emp/delete/{id}")
public String delete(@PathVariable("id")Integer id, Model model){
employeeDao.deleteEmployeeById(id);
return "redirect:/emps";
}
四、404处理
至此,增删改查操作全部结束!接下来看看如何定义404错误?
通过SpringBoot自定义404错误非常简单,我们只需要在templates文件夹下定义一个error目录,把对应的404页面放到error目录下,SpringBoot就会自动帮我们配置404页面,配置500页面原理也是如此。
五、注销操作
前端:
<a class="nav-link" th:href="@{/user/logOut}">注销</a>
后端controller:
//注销操作
@GetMapping("/user/logOut")
public String logOut(HttpSession session){
//清除session域
session.invalidate();
return "redirect:/index.html";
}