首先简单地创建一个SpringBoot项目
导入文件
因为这节主要说说如何引入资源文件,譬如登录页面,所以实体类、dao层的编写不详细说,
链接:https://pan.baidu.com/s/16q6mMMLSzg-YYPMap-pF-w
提取码:1234
导入依赖
因为此项目,页面均采用thymeleaf模板,所以要导入spring-boot-starter-thymeleaf
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
引入资源文件
假设你看过12 | SpringBoot对静态资源的映射规则这篇文章,你就会知道引入资源文件除了可以在controller返回指定的资源文件,还可编写一个配置类,因为WebMvcConfigurationAdapter 在spring boot 2.0被废弃了,所以我们要实现WebMvcConfigurer接口
//使用WebMvcConfigurer可以来扩展SpringMVC的功能
//@EnableWebMvc 不要接管SpringMVC
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("login");
registry.addViewController("/index.html").setViewName("login");
}
}
这样,在浏览器中输入 http://localhost:8080/index.html 或者 http://localhost:8080即可访问登录页面
最终的项目结构