SpringBoot------员工管理系统:准备工作(十四)
狂神的教程中的模板资源,自己没有,就在bootstrap模板网站上下载了一个后台管理的模板,用来练手。
免费的bootstrap模板网站
https://themefisher.com/free-bootstrap-templates/
https://gridgum.com/themes/category/free/
https://startbootstrap.com/template-categories/all/
模拟数据库
部门
/**
* 部门dao
*/
public class DepartmentDao {
//模拟数据库操作
private static Map<Integer, Department> departmentMap = null;
static {
departmentMap = new HashMap<Integer, Department>();
departmentMap.put(100,new Department(100,"教学部"));
departmentMap.put(101,new Department(101,"教研部"));
departmentMap.put(102,new Department(102,"后勤部"));
departmentMap.put(103,new Department(103,"纪律部"));
departmentMap.put(104,new Department(104,"宣传部"));
departmentMap.put(105,new Department(105,"市场部"));
}
//查找所有的部门
public Collection<Department> getDepartments(){
return departmentMap.values();
}
//根据ID获取当前部门
public Department getDepartmentById(Integer id){
return departmentMap.get(id);
}
}
员工
package com.example.demo.dao;
import com.example.demo.pojo.Department;
import com.example.demo.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* 员工dao
*/
@Repository
public class EmployeeDao {
//模拟数据库操作
private static Map<Integer, Employee> employeeMap = null;
@Autowired
private DepartmentDao departmentDao;
static {
employeeMap = new HashMap<Integer, Employee>();
//这个Department不能通过----departmentDao.getDepartmentById()方法获取,因为static优先加载,departmentDao之后加载,获取不到
employeeMap.put(1,new Employee(1,"陈一","[email protected]",1,new Department(100,"教学部"),new Date()));
employeeMap.put(2,new Employee(2,"陈二","[email protected]",0,new Department(101,"教研部"),new Date()));
employeeMap.put(3,new Employee(3,"陈三","[email protected]",1,new Department(102,"后勤部"),new Date()));
employeeMap.put(4,new Employee(4,"陈四","[email protected]",0,new Department(103,"纪律部"),new Date()));
employeeMap.put(5,new Employee(5,"陈五","[email protected]",1,new Department(104,"宣传部"),new Date()));
employeeMap.put(6,new Employee(6,"陈六","[email protected]",0,new Department(105,"市场部"),new Date()));
}
//初始化Id,用于自增操作
private static Integer initId = 7;
//保存Employee
public void save(Employee e){
if (e == null){
e.setId(initId++);
}
e.setDepartment(departmentDao.getDepartmentById(e.getDepartment().getId()));
employeeMap.put(e.getId(),e);
}
//查找所有的员工信息
public Collection<Employee> getEmployees(){
return employeeMap.values();
}
//根据ID获取员工信息
public Employee getEmployeesById(Integer id){
return employeeMap.get(id);
}
//根据ID删除员工
public void removeByID(Integer id){
employeeMap.remove(id);
return;
}
}
模板首页
所有页面的静态资源都需要使用thymeleaf接管
//全面扩展SpringMVC.
//将这个类变成配置类
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
//视图跳转
//想要通过地址zhaoyun访问到success页面,那么这个页面必须加入<html lang="en" xmlns:th="http://www.thymeleaf.org"> 并且在templates目录下
//跟目录的在这里配置会好一点,但是静态资源加载不到
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
}
}
曝问题:No mapping for GET /js/scripts.js
SpringBoot,使用thymeleaf加载不到静态资源。
关注点:
1.自定义的拦截器类:继承WebMvcConfigurer。
2.自定义的拦截器类:加注解@Configuration。
3.自定义的拦截器类:不能加注解@EnableWebMvc。
4.<html lang="en" xmlns:th="http://www.thymeleaf.org">
html标签加上thymeleaf,导入命名空间。
5.link标签改变为:<link th:href="@{/css/monokai.css}" rel="stylesheet">
,th:href以及**@{}**。/就已经能从rescourse目录下查找。
6.thymeleaf可能存在缓存,将缓存设为fasle。
# 关闭模板引擎的缓存
spring:
thymeleaf:
cache: false
@{/}:是万能匹配,即使在配置文件中修改访问路径,依然能找到对应的静态资源。
在配置文件中添加配置:
server:
servlet:
context-path: /zhaoyun
在页面中访问时,能够看到静态资源的加载路径都会自动加上所配置的路径。
扫描二维码关注公众号,回复:
13255033 查看本文章