• SpringBoot导入Thymeleaf


    SpringBoot导入Thymeleaf


    模版引擎

    前端交给我们的页面,是html页面。如果是我们以前开发,我们需要把他们转成jsp页面,jsp好处就是 当我们查出一些数据转发到JSP页面以后,我们可以用jsp轻松实现数据的显示,及交互等。

    jsp支持非常强大的功能,包括能写Java代码,但是呢,我们现在的这种情况,SpringBoot这个项目首先 是以jar的方式,不是war,像第二,我们用的还是嵌入式的Tomcat,所以呢,SpringBoot现在默认是不支持jsp 的。

    SpringBoot推荐你可以来使用模板引擎:

    模板引擎,我们其实大家听到很多,其实jsp就是一个模板引擎,还有以用的比较多的freemarker,包括 SpringBoot给我们推荐的Thymeleaf,模板引擎有非常多,但再多的模板引擎,他们的思想都是一样的。

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

    简单来说,模版引擎就是帮助我们已一定的格式解析后台的数据

    引入Thymeleaf

    Thymeleaf官网
    Thymeleaf Github

    找到对应的pom依赖

    
    <dependency>
    	<groupId>org.springframework.bootgroupId> 
    	<artifactId>spring-boot-starter-thymeleafartifactId>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    查看一下Thymeleaf的自动配置类:ThymeleafProperties

    @ConfigurationProperties( prefix = "spring.thymeleaf"
    )
    public class ThymeleafProperties {
    	private static final Charset DEFAULT_ENCODING;
    	public static final String DEFAULT_PREFIX = "classpath:/templates/"; public static final String DEFAULT_SUFFIX = ".html";
    	private boolean checkTemplate = true;
    	private boolean checkTemplateLocation = true;
    	private String prefix = "classpath:/templates/";
    	private String suffix = ".html";
    	private String mode = "HTML";
    	private Charset encoding;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    我们可以在其中看到默认的前缀和后缀!
    我们只需要把我们的html页面放在类路径下的templates下,thymeleaf就可以帮我们自动渲染了。
    使用thymeleaf什么都不需要配置,只需要将他放在指定的文件夹下即可!

    Demo测试

    在这里插入图片描述
    test.html

    DOCTYPE html>
    <html xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>Titletitle>
    head>
    <body>
    	<div th:text="${msg}">div>
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    IndexController

    @Controller
    public class IndexController {
        @RequestMapping("/test")
        public String test(Model model){
            model.addAttribute("msg","hello,springboot!!!");
            return "test";
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    在这里插入图片描述

    如果对您有帮助,免费的赞点一个~~~感谢🙏

    在这里插入图片描述

  • 相关阅读:
    【21】c++设计模式——>装饰模式
    mysql8绿色版安装教程
    【心得】基于flask的SSTI个人笔记
    独立站:最新选品建议
    UID、EUID、GID和EGID
    成功编译并运行flutter安卓的gradle文件范例
    java计算机毕业设计ssm课程建设制作服务平台系统
    大数据周会-本周学习内容总结017
    淘宝/天猫API 获得淘宝app商品详情原数据(优惠券详情)
    Java Day9 Stream流
  • 原文地址:https://blog.csdn.net/qq_41359998/article/details/123179255