SpringBoot-04-web(显示登录页面)

1、静态资源导入

方法一:
  SpringBoot中,SpringMVC的web配置都在 WebMvcAutoConfiguration 这个配置里面,我们可以去看看 WebMvcAutoConfigurationAdapter 中有很多配置方法;

public void addResourceHandlers(ResourceHandlerRegistry registry) {
            if (!this.resourceProperties.isAddMappings()) {
                logger.debug("Default resource handling disabled");
            } else {
                Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
                CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
                if (!registry.hasMappingForPattern("/webjars/**")) {
                    this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
                }

                String staticPathPattern = this.mvcProperties.getStaticPathPattern();
                if (!registry.hasMappingForPattern(staticPathPattern)) {
                    this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
                }

            }
        }

  可以看到,默认都是去这个路径下找静态文件:classpath:/META-INF/resources/webjars/"。此时我们还没有这个包,那我们就去导入依赖。

<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>jquery</artifactId>
    <version>3.4.1</version>
</dependency>

  访问:只要是静态资源,SpringBoot就会去对应的路径寻找资源,我们这里访问 :http://localhost:8080/webjars/jquery/3.3.1/jquery.js

方法二:
  那我们项目中要是使用自己的静态资源该怎么导入呢?我们看下一行代码;我们去找staticPathPattern发现第二种映射规则 : /** , 访问当前的项目任意资源,它会去找 resourceProperties 这个类,我们可以点进去看一下,

@ConfigurationProperties(
    prefix = "spring.resources",
    ignoreUnknownFields = false
)
public class ResourceProperties {
    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};

  resourceProperties 可以设置和我们静态资源有关的参数,this.staticLocations = CLASSPATH_RESOURCE_LOCATIONS; 这里面指向了它会去寻找资源的文件夹;即上面数组的内容。

   **而放在这里面的静态资源是能够被直接访问接收的。**因此直接访问:http://localhost:8080/hello.js

总结: 以下四个路径都可以放静态资源

  • “classpath:/META-INF/resources/”,(系统给的,不常用)
  • “classpath:/resources/”(优先级1):上传的一些文件
  • “classpath:/static/” (优先级2):可以放一些图片
  • “classpath:/public/” (优先级3):常用于一些公共资源

补充:

  • 首页文件名称:index.html,也可以放到以上文件中进行直接访问。

注意事项:

  • 当你在配置文件中设置:**spring.mvc.static-path-pattern=(文件路径)**之后以上所有的方法失效。
  • 调试过程中发现静态资源无法提交更新,设置两个地方:1,在配置中将每次提交更改为:class and resources.2:设置浏览器中的 network-disable cache

2、thymeleaf模板引擎

在这里插入图片描述
  模板引擎的作用就是我们来写一个页面模板,比如有些值呢,是动态的,我们写一些表达式。而这些值,从哪来呢,我们来组装一些数据,我们把这些数据找到。然后把这个模板和这个数据交给我们模板引擎,模板引擎按照我们这个数据帮你把这表达式解析、填充到我们指定的位置,然后把这个数据最终生成一个我们想要的内容给我们写出去,这就是我们这个模板引擎,不管是jsp还是其他模板引擎,都是这个思想。

  在使用之前导入依赖,当然你也可以在建立项目的时候就导入他

<!--thymeleaf模板-->
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring5</artifactId>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-java8time</artifactId>
        </dependency>

注意事项:

  • 使用thymeleaf之后你的index.html就需要放在templates这个文件夹之下,通过controller进行跳转访问了。
  • 使用thymeleaf之后,对应的html文件就需要做相应的改变,使其满足相应的语法。

2、开发前准备

2.1 创建相应的数据库和实体类

  这个也就是对应着pojo和dao两个文件夹。
Department类

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

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Department {
    private Integer id;
    private String departmentName;
}

Employee类

package com.lucius.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
@Data
@NoArgsConstructor
public class Employee {
    private Integer id;
    private Integer gender;//0:女 1:男
    private String name;
    private String email;
    private Department department;
    private Date birth;

    public Employee(Integer id, Integer gender, String name, String email, Department department) {
        this.id = id;
        this.gender = gender;
        this.name = name;
        this.email = email;
        this.department = department;
        this.birth = new Date();
    }
}

注意事项:

  • 在有参构造中,没有使用lombok注解,因为我想要让日期能够自行生成,因此此处的Date是java.util.Date。并且如此一来有参构造会少一个参数。

EmployeeDao

package com.lucius.dao;

import com.lucius.pojo.Department;
import com.lucius.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

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

@Repository
public class EmployeeDao {
    //模拟数据库中的数据
    //static关键字
    private static Map<Integer, Employee> employees = null;

    @Autowired
    private DepartmentDao departmentDao;

    static {
        employees=new HashMap<Integer, Employee>();//创建一个员工表

        employees.put(1,new Employee(0001,1,"A","A123@163.com",new Department(101,"教学部")));
        employees.put(2,new Employee(0002,0,"B","B123@163.com",new Department(102,"后勤部")));
        employees.put(3,new Employee(0003,1,"C","C123@163.com",new Department(103,"财务部")));
        employees.put(4,new Employee(0004,0,"D","D123@163.com",new Department(104,"运营部")));
        employees.put(5,new Employee(0005,1,"E","E123@163.com",new Department(105,"宣传部")));

    }
    //自增主键
    private Integer initId=6;
    //增加一个员工

    public void addEmployee(Employee employee){
        if(null==employee.getId()){
            employee.setId(initId++);
        }

        employee.setDepartment(departmentDao.getDepartmentById(employee.getDepartment().getId()));

        employees.put(employee.getId(),employee);
    }

    //删除一个员工
    public void deleteEmployeeById(int id){

        employees.remove(id);
    }
    //查询所有员工
    public Collection<Employee> queryAllEmployee(){
        return employees.values();
    }
    //通过id查询员工
    public Employee queryEmployeeById(int id){
        return employees.get(id);
    }

}

DepartmentDao

package com.lucius.dao;

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

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

//部门dao
//被spring托管
@Repository
public class DepartmentDao {

    //模拟数据库中的数据
    //static关键字
    private static Map<Integer, Department> departmens = null;

    static {
        departmens=new HashMap<Integer, Department>();//创建一个部门表

        departmens.put(01,new Department(101,"教学部"));
        departmens.put(02,new Department(102,"后勤部"));
        departmens.put(03,new Department(103,"财务部"));
        departmens.put(04,new Department(104,"运营部"));
        departmens.put(05,new Department(105,"宣传部"));

    }

    //获得所有部门信息
    public Collection<Department> getDepartments(){
        return departmens.values();
    }
    //通过ID查询部门
    public Department getDepartmentById(Integer id){
        return departmens.get(id);
    }
}

注意事项:

  • 在构建数据库的时候,我们使用了static关键字!

  这样底层就差不多写好了,但是以后肯定是去配置mybatis而不是自己写数据库了。

  说明:由于重点不是前端,因此对于前端文件都直接导入。
在这里插入图片描述

2.2自定义mvc配置

  在这个配置文件中,我们可以进行额外的配置,@Configuration说明了这是一个配置文件;注意要实现WebMvcConfigurer接口。
  首先我们就进行了首页设置:

@Configuration
public class MyMvcConfig implements WebMvcConfigurer {

    //自己去定义首页跳转
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //增加视图解析器
        registry.addViewController("/").setViewName("index");
        registry.addViewController("/index.html").setViewName("index");
    }

  于是我们访问localhost:8080/或者localhost:8080/index.html
就可以访问到我们的首页。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值