• SpringBoot-14-模块开发-环境和首页


    5.6.1 环境准备

    我们可以使用开源的前端页面模板,里面有非常优秀的静态资源页面和js,css等资源,例如:php中文网,本节只是演示SpringBoot进行web模块开发的过程

    • 模块结构
      在这里插入图片描述

    • 实体类pojo目录

    • Department/Employee

    package com.zk.pojo;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    /**
     * @author CNCLUKZK
     * @create 2022/7/31-22:55
     */
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class Department {
        private Integer id;
        private String departmentName;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    package com.zk.pojo;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    import java.util.Date;
    
    /**
     * @author CNCLUKZK
     * @create 2022/7/31-22:56
     */
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class Employee {
        private Integer id;
        private String lastName;
        private String email;
        /**
         * 0:女 1:男
         */
        private Integer gender;
        private Department department;
        private Date birth;
    }
    
    • 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
    • dao层:DepartmentDao、EmployeeDao用map来模拟数据的实现
    package com.zk.dao;
    
    import com.zk.pojo.Department;
    import org.springframework.stereotype.Repository;
    
    import java.util.Collection;
    import java.util.HashMap;
    import java.util.Map;
    
    /**
     * @author CNCLUKZK
     * @create 2022/7/31-23:03
     */
    @Repository
    public class DepartmentDao {
        //模拟数据库中的数据
        private static Map departmentMap = null;
        static {
            departmentMap = new HashMap<>();
            departmentMap.put(101,new Department(101,"教学部"));
            departmentMap.put(101,new Department(102,"统战部"));
            departmentMap.put(101,new Department(103,"市场部"));
            departmentMap.put(101,new Department(104,"后勤部"));
            departmentMap.put(101,new Department(105,"运营部"));
            departmentMap.put(101,new Department(106,"编辑部"));
        }
    
        public Collection getDepartments(){
            return departmentMap.values();
        }
    
        public Department getDepartmentById(Integer id){
            return departmentMap.get(id);
        }
    }
    
    
    • 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.zk.dao;
    
    import com.zk.pojo.Department;
    import com.zk.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 CNCLUKZK
     * @create 2022/7/31-23:12
     */
    @Repository
    public class EmployeeDao {
        private static Map employeeMap = null;
    
        @Autowired
        private DepartmentDao departmentDao;
    
        static {
            employeeMap = new HashMap<>();
            //就是alt加选中--批量操作
            employeeMap.put(1001,new Employee(1001,"tony","11@163.com",1,new Department(101,"教学部"),new Date()));
            employeeMap.put(1002,new Employee(1002,"mary","11@163.com",0,new Department(102,"统战部"),new Date()));
            employeeMap.put(1003,new Employee(1003,"tom","11@163.com",1,new Department(103,"市场部"),new Date()));
            employeeMap.put(1004,new Employee(1004,"hanni","11@163.com",0,new Department(104,"后勤部"),new Date()));
            employeeMap.put(1005,new Employee(1005,"wanghua","11@163.com",1,new Department(105,"运营部"),new Date()));
            employeeMap.put(1006,new Employee(1006,"hanmeimei","11@163.com",0,new Department(106,"编辑部"),new Date()));
        }
    
        public Collection getEmployees(){
            return employeeMap.values();
        }
        private Employee getEmployeeById(Integer id){
            return employeeMap.get(id);
        }
    
        private static Integer eid = 1007;
    
        public void saveEmployee(Employee employee){
            if (employee.getId() == null) {
                employee.setId(eid++);
            }
            employee.setDepartment(departmentDao.getDepartmentById(employee.getDepartment().getId()));
            employeeMap.put(employee.getId(),employee);
        }
    
        public void updateEmployee(Employee employee){
            if (employee.getId() == null) {
                employee.setId(eid++);
            }
            employeeMap.put(employee.getId(),employee);
        }
    
        public void delateEmployeeById(Integer id){
            employeeMap.remove(id);
        }
    }
    
    
    • 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
    • 59
    • 60
    • 61
    • 62
    • 63
    5.6.2 首页实现
    • 方式1:首页跳转实现Controller中写请求路径
    @Controller
    public class LoginController {
        @RequestMapping({"/","/index"})
        public String toindex(){
            return "index";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 方式2:自定义扩展springmvc
    package com.zk.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;
    //扩展springmvc
    //如果。你想diy一些定制化的功能,只要写这个组件,然后将它交给springboot,springboot就会帮我们自动装魔!
    //应为类型要求为WebMvcConfigurer,所以我们实现其接口
    //可以使用自定义类扩展springmvc的功能
    @Configuration
    @EnableWebMvc
    public class MyMvcConfiguration implements WebMvcConfigurer {
    
        //视图跳转
        @Override
        public void addViewControllers(ViewControllerRegistry registry) {
            //浏览器发送特殊请求,就会跳转到特定页面
            registry.addViewController("/").setViewName("index");
            registry.addViewController("/index").setViewName("index");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 关闭模板引擎的缓存
    #关闭模板引擎的缓存
    spring:
        thymeleaf:
        	cache: false
    
    • 1
    • 2
    • 3
    • 4
    • 首页index.html
    
    
    	
    		
    		
    		
    		
    		Signin Template for Bootstrap
    		
    		
    		
    		
    	
    
    	
    		
    
    	
    
    
    
    • 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
    • 仅仅用thymeleaf修改本地链接,在线链接不要改
    • 首页配置:注意点,所有页面的静态资源都需要使用thymeleaf接管;链接@{}
    server:
      port: 8080
      servlet:
        context-path: /zk
    
    • 1
    • 2
    • 3
    • 4
    • thymeleaf好处:一旦修改了项目根地址,静态资源即可自动加上根地址http://127.0.0.1:8081/zk/asserts/css/bootstrap.min.css
    下一篇:SpringBoot-15-模块开发-页面国际化
  • 相关阅读:
    spring源码解析、并发编程实战实践等深度进阶电子文档分享
    记Windows服务器Redis 6379被攻击 被设置主从模式同步项目数据
    Mac上怎么合并多张图片?
    js设计模式
    应用部署引起上游服务抖动问题分析及优化实践方案
    LVS集群
    LeetCode每日一题——805. 数组的均值分割
    企业微信hook接口协议,ipad协议http,发送大视频文件
    Linux之计划任务
    FPGA-出租车计价器的实现
  • 原文地址:https://blog.csdn.net/weixin_42045639/article/details/126445798