前言:

本文内容:扩展SpringMVC、员工管理系统:准备工作、首页实现

推荐免费SpringBoot基础教程视频:【狂神说Java】SpringBoot最新教程通俗易懂_哔哩哔哩_bilibili

扩展SpringMVC

MyMVCConfig.java下编写视图跳转

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.jokerdig.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


/**
* @author Joker大雄
* @data 2022/7/16 - 13:03
**/
// 扩展SpringMVC 官方推荐使用@Configuration这种方式
// 如果想要自定义一些功能,只要写好组件,然后将它交给SpringBoot,它就帮我们自动装配
// 自定义功能不能添加@EnableWebMvc注解,根据源码分析出只要添加该注解,自定义配置就失效了
@Configuration
public class MyMVCConfig implements WebMvcConfigurer {

// 视图跳转
@Override
public void addViewControllers(ViewControllerRegistry registry) {
// 浏览器发送 jokerdig,就会跳转到test页面
registry.addViewController("/jokerdig").setViewName("test");
}
}

运行测试

http://localhost:8080/jokerdig

1
Test页面

员工管理系统:准备工作

删除springboot-03-web项目多余内容

项目准备工作

  1. 下载页面静态资源,将页面放置在templates下,资源文件放置在static

    百度云下载 提取码:npcw

  2. 新建com.jokerdig.pojo包,在包下新建Department.javaEmployee.java,并在pom.xml引入lombok

    Department.java

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    package com.jokerdig.pojo;

    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;

    /**
    * @author Joker大雄
    * @data 2022/7/18 - 9:59
    **/
    // 部门类
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class Department {
    private Integer id;
    private String departmentName;

    }

    Employee.java

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    package com.jokerdig.pojo;

    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;

    import java.util.Date;

    /**
    * @author Joker大雄
    * @data 2022/7/18 - 10:03
    **/
    // 员工类
    @Data
    @NoArgsConstructor
    public class Employee {
    private Integer id;
    private String lastName;
    private String email;
    private Integer gender; // 0女 1男
    private Department department;
    private Date birth;

    public Employee(Integer id, String lastName, String email, Integer gender, Department department) {
    this.id = id;
    this.lastName = lastName;
    this.email = email;
    this.gender = gender;
    this.department = department;
    // 自动生成date
    this.birth = new Date();
    }
    }
  3. 新建com.jokerdig.dao包,包下新建DepartmentDao.javaEmployeeDao.java并模拟数据

    DepartmentDao.java

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    package com.jokerdig.dao;

    import com.jokerdig.pojo.Department;
    import org.springframework.stereotype.Repository;

    import java.util.Collection;
    import java.util.HashMap;
    import java.util.Map;

    /**
    * @author Joker大雄
    * @data 2022/7/18 - 10:36
    **/
    // 部门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> getDepartments(){
    return departments.values();
    }
    // 通过id得到部门
    public Department getDepartmentById(Integer id){
    return departments.get(id);
    }
    }

    EmployeeDao.java

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    package com.jokerdig.dao;


    import com.jokerdig.pojo.Department;
    import com.jokerdig.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;

    /**
    * @author Joker大雄
    * @data 2022/7/18 - 10:37
    **/
    // 员工dao
    @Repository
    public class EmployeeDao {
    // 模拟数据库数据
    private static Map<Integer, Employee> employees = null;
    // 员工有所属的部门
    @Autowired
    private DepartmentDao departmentDao;

    static{
    employees = new HashMap<Integer,Employee>();
    employees.put(1001,new Employee(1001,"AA","aa@gmail.com",1,new Department(101,"教学部")));
    employees.put(1002,new Employee(1002,"BB","bB@gmail.com",0,new Department(102,"生活部")));
    employees.put(1003,new Employee(1003,"CC","cC@gmail.com",1,new Department(103,"体育部")));
    employees.put(1004,new Employee(1004,"DD","dD@gmail.com",0,new Department(104,"后勤部")));
    employees.put(1005,new Employee(1005,"EE","EE@gmail.com",1,new Department(105,"运营部")));
    }
    //主键自增
    private static Integer initId = 1006;
    // 添加和修改员工
    public void save(Employee employee){
    if(employee.getId()==null){
    employee.setId(initId++);
    }
    employee.setDepartment(departmentDao.getDepartmentById(employee.getDepartment().getId()));

    employees.put(employee.getId(),employee);
    }
    // 查询所有员工信息
    public Collection<Employee> getAll(){
    return employees.values();
    }
    // 通过id查询员工
    public Employee getEmployeeById(Integer id){
    return employees.get(id);
    }
    // 删除员工
    public void delete(Integer id){
    employees.remove(id);
    }
    }
  4. 准备工作完成

员工管理系统:首页实现

  1. 编写com.jokerdig.config包下的MyMVCConfig.java

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    package com.jokerdig.config;

    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


    /**
    * @author Joker大雄
    * @data 2022/7/16 - 13:03
    **/
    // 扩展SpringMVC 官方推荐使用@Configuration这种方式
    @Configuration
    public class MyMVCConfig implements WebMvcConfigurer {
    // 配置首页
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/").setViewName("index");
    registry.addViewController("/index.html").setViewName("index");
    }
    }

    运行http://localhost:8080/

    发现首页可以显示,但是静态资源没有加载,因为我们要使用Thymeleaf

  2. 使用Thymeleaf

    打开index.html并按照如下修改

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    <!DOCTYPE html>
    <!--引入thymeleaf的约束-->
    <html lang="en" xmlns:th="http://www.thymeleaf.org"
    xmlns:sec="http://www.thymeleaf.org/extras/spring-security"
    xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>首页</title>
    <!-- 通过 thymeleaf 语法引入链接-->
    <!-- Bootstrap core CSS -->
    <link th:href="@{/css/bootstrap.min.css}" rel="stylesheet">
    <!-- Custom styles for this template -->
    <link th:href="@{/css/signin.css}" rel="stylesheet">
    <!-- 自定义图标-->
    <link rel="icon" th:href="@{/favicon.ico}">
    </head>

    <body class="text-center">
    <form class="form-signin" action="dashboard.html">
    <img class="mb-4" th:src="@{/img/bootstrap-solid.svg}" alt="" width="72" height="72">
    <h1 class="h3 mb-3 font-weight-normal">Please sign in</h1>
    <label class="sr-only">Username</label>
    <input type="text" class="form-control" placeholder="Username" required="" autofocus="">
    <label class="sr-only">Password</label>
    <input type="password" class="form-control" placeholder="Password" required="">
    <div class="checkbox mb-3">
    <label>
    <input type="checkbox" value="remember-me"> Remember me
    </label>
    </div>
    <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
    <p class="mt-5 mb-3 text-muted">© 2017-2022</p>
    <a class="btn btn-sm">中文</a>
    <a class="btn btn-sm">English</a>
    </form>
    </body>
    </html>

    如果修改后浏览器无效果,可以在application.properties中关闭模板引擎缓存并清理浏览器缓存

    1
    2
    # 关闭模板引擎缓存
    spring.thymeleaf.cache=false

    运行首页可以正常显示

  3. 把其他页面都修改好,主要是修改本地的url地址格式

  4. 首页完成