程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

2023-06(2)

SpringBoot------员工管理系统:准备工作以及首页(十四)

发布于2021-05-29 22:46     阅读(772)     评论(0)     点赞(17)     收藏(0)


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,"陈一","1@qq.com",1,new Department(100,"教学部"),new Date()));
        employeeMap.put(2,new Employee(2,"陈二","2@qq.com",0,new Department(101,"教研部"),new Date()));
        employeeMap.put(3,new Employee(3,"陈三","3@qq.com",1,new Department(102,"后勤部"),new Date()));
        employeeMap.put(4,new Employee(4,"陈四","4@qq.com",0,new Department(103,"纪律部"),new Date()));
        employeeMap.put(5,new Employee(5,"陈五","5@qq.com",1,new Department(104,"宣传部"),new Date()));
        employeeMap.put(6,new Employee(6,"陈六","6@qq.com",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

在页面中访问时,能够看到静态资源的加载路径都会自动加上所配置的路径。

在这里插入图片描述



所属网站分类: 技术文章 > 博客

作者:听说你很拽

链接:http://www.javaheidong.com/blog/article/207908/4b9207ea063e1f181f9d/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

17 0
收藏该文
已收藏

评论内容:(最多支持255个字符)