springboot 分析源码欢迎页和图标-> thymeleaf模板引擎常用语法->扩展

发布时间 2023-05-04 19:22:26作者: 醒醒起来

欢迎页:

 

icon:

 注意点:

 thymeleaf模板引擎

1.使用thymeleaf模板引擎前要导入对应依赖包

2.阅读源码:

根据源码说明我们可以将html文件放置在templates目录下,然后通过controller进行跳转即可

 controller类:

//在templates下的东西需要通过controller类来跳转,
// 需要导入thymeleaf对应的依赖包
@Controller
public class IndexController {
    //跳转到templates目录下的测试页面
    @RequestMapping("/t")
    public String test1(){
        return "test";
    }
}
View Code

 

结果:

 thymeleaf语法介绍

这里测试一下在controller类设置一个属性给前端接收并展示,

首先要搞清楚在thymeleaf中是如何在前端页面展示接收到的数据的,查看源码发现标签th:text和th:each

html代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>test thymeleaf!!</h1>
<div th:text="${msg}"></div>
<div th:each="user:${users}" th:text="${user}"></div>
</body>
</html>
View Code

controller测试代码:

//在templates下的东西需要通过controller类来跳转,
// 需要导入thymeleaf对应的依赖包
@Controller
public class IndexController {
    //跳转到templates目录下的测试页面
    @RequestMapping("/t1")
    public String test1(){
        return "test";
    }
    @RequestMapping("/t2")
    public String test2(Model model){
        //在这里设置一个属性给前端接收并展示
        model.addAttribute("msg","test2---hello,thymeleaf!");
        return "test";

    }
    @RequestMapping("/t3")
    public String test3(Model model){
        //在这里设置一个属性给前端接收并展示
        model.addAttribute("msg","<h1>test3---hello,thymeleaf!</h1>");
        model.addAttribute("users", Arrays.asList("lian","xiaoming","xiaohong"));
        return "test";
    }
}
View Code

 

 运行结果(部分):

总结一些常用的thymeleaf标签:

th:text="${emp.getId()}" 将数据以文本形式展示在前端

th:each="emp:${emps}"遍历列表emps中的每一项

th:checked="${emp.getGender()==0}"判断,暂时不清楚

th:value="${#dates.format(emp.getBirth(),'yyyy-MM-dd')}" 暂时不太清楚,暂且理解的是取出数据传递给后端

 

扩展-> springmvc配置原理源码:

注意点:springmvc的配置在springboot也要用到自动装配类WebMvcAutoConfiguration

我们可以选择直接继承类WebMvcConfigurer并重写其中的方法

或者自己重新自定义thymeleaf也有自定义的视图解析器,但是如果需要自定义一些功能,可以写组件继承类WebMvcConfiguer

@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
   // WebMvcAutoConfiguration
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/lian").setViewName("test");//视图跳转,相当于请求转发
    }
    //ViewResolver实现了视图解析器接口的类,我们就可以把它看成视图解析器
    @Bean
    public ViewResolver viewResolver(){
        return new MyViewResolver();
    }

    //自定义一个自己的视图解析器MyViewResolver
    public static class MyViewResolver implements ViewResolver{
        @Override
        public View resolveViewName(String s, Locale locale) throws Exception {
            return null;
        }
    }
}
View Code

官方doc指出的问题:

 这是因为:

我们diy的自动配置类也是需要通过WebMvcAutoConfiguration类将我们diy的自动配置类给springboot托管的,WebMvcAutoConfiguration类中有个静态内部类WebMvcAutoConfigurationAdapter,这个类导入了EnableWebMvcConfiguration.class

 这个EnableWebMvcConfiguration类继承了DelegatingWebMvcConfiguration

 然而在DelegatingWebMvcConfiguration类中我们发现它继承了 WebMvcConfigurationSupport类

 

而在我们最开始的要保证类失效,必须满足WebMvcAutoConfiguration类能正常生效,但是WebMvcAutoConfiguration类要求不存在WebMvcConfigurationSupport类,这与我们上面说的冲突了,所以官方要求不加@EnableWebMvc

 =====================================分割线===================================================