目录
2、将导入的index.html首页文件改成thymeleaf支持的样子
1、在MyMvcConfig中配置视图跳转
(因为页面是放在templates下面的,这个文件夹里面的页面无法直接访问,只能通过controller控制访问)
package com.zhou.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
// 如果我们要扩展springmvc,官方建议我们这样去做
// 如果 你想diy一些定制化的功能,只要写这个组件,然后将它交给springboot,springboot就会帮我们自动装配
// 扩展springmvc dispatcherservlet
@Configuration
// @EnableWebMvc 这玩意就是导入了一个类: DelegatingWebMvcConfiguration:从容器中获取所有度webmvcconfig 添加了这个注解,自动配置就全被失效
public class MyMvcConfig implements WebMvcConfigurer {
// 视图跳转
@Override
public void addViewControllers(ViewControllerRegistry registry){
registry.addViewController("/").setViewName("index"); // 只要页面请求是/就是进首页
registry.addViewController("/index.html").setViewName("index"); // 只要页面请求是/index.html就是进首页
registry.addViewController("/main.html").setViewName("dashboard");
}
}
2、将导入的index.html首页文件改成thymeleaf支持的样子
头部添加代码 xmlns:th="http://www.thymeleaf.org" 并将所有的本地文件引用改成thymeleaf的格式
3、关闭模板引擎缓存
4、同样把所有的页面都改成thymeleaf支持的格式
5、注意点