springboot员工管理系统:实现首页
本项目参考狂神老师的springboot项目,项目具体讲解视频请参考:
https://www.bilibili.com/video/BV1PE411i7CV?t=5&p=20
首页的实现我们主要有两种方式
方式一:写一个controller实现
//会解析到templates目录下的index.html页面
@RequestMapping({"/","/index.html"})//这里采用数组的形式来令其多个路径同时访问到index界面
public String index(){
return "index";
}
方式二:自己编写MVC的扩展配置
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {//添加视图控制器
//添加两个路径以便让视图控制器跳转到指定页面
registry.addViewController("/").setViewName("index");
registry.addViewController("/index.html").setViewName("index");
}
}
解决了首页问题,我们还需要解决一个资源导入的问题;
为了保证资源导入稳定,我们建议在所有资源导入时候使用 th:去替换原有的资源路径!这也是模板规范
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<link th:href="@{/asserts/css/bootstrap.min.css}" rel="stylesheet">
此外我们还可以做一些其他的配置
比如:关闭模板引擎的缓存、网页我们可以自定义访问路径等
# 关闭模板引擎的缓存
spring.thymeleaf.cache=false
# 网页前自定义路径
server.servlet.context-path=/hsy
ok,这样我们就实现了首页~