首页 > 其他分享 >SpringBoot集成Thymeleaf

SpringBoot集成Thymeleaf

时间:2023-01-16 11:02:08浏览次数:52  
标签:集成 SpringBoot nekohtml ## spring boot thymeleaf Thymeleaf 模板

1、引入依赖

在maven项目的pom.xml中引入spingboot-boot-starter-thymeleaf依赖,同时为了解决Html严格校验报错的问题,增加nekohtml依赖

复制代码
<!--thymeleaf模板引擎-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <dependency>
            <groupId>net.sourceforge.nekohtml</groupId>
            <artifactId>nekohtml</artifactId>
            <version>1.9.15</version>
        </dependency>
复制代码

2、配置application.properties

复制代码
server.port=8080

##取出thymeleaf的html的严格校验
spring.thymeleaf.mode=LEGACYHTML5

##设定thymeleaf文件路径,默认为sc/main/resources/templates
spring.thymeleaf.prefix=classpath:/templates/

##设定静态文件路径,js、css等
spring.mvc.static-path-pattern=/static/**

##是否开启模板缓存,默认true
##建议在开发时关闭模板缓存,不然无法看到实时的页面
spring.thymeleaf.cache=false

##模板编码
spring.thymeleaf.encoding=UTF-8
复制代码

3、编写demo

(1)Controller控制层

复制代码
@Controller
public class TestController {

    @RequestMapping("/")
    public String testThymeleaf(ModelMap modelMap){
        modelMap.addAttribute("msg", "Hello , this is thymeleaf");
        return "thymeleaf";
    }
复制代码

(2)Html页面

复制代码
<!DOCTYPE html>
<!--解决th报错 -->
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>thymeleaf</title>
</head>
<body>
    <h1 th:text="${msg}"></h1>
</body>
</html>
复制代码

访问:http://localhost:8080

 返回顶部

   

标签:集成,SpringBoot,nekohtml,##,spring,boot,thymeleaf,Thymeleaf,模板
From: https://www.cnblogs.com/kn-zheng/p/17054897.html

相关文章

  • springboot集成nacos 注册中心
     接上一篇集成配置中心,本文介绍注册中心,目录结构如下在nacosregister的pom.xml文件中添加引用<dependency><groupId>com.alibaba.cloud</groupId>......
  • springboot反射 + 自定义注解
    利用反射调用方法获取bean寻找bean中指定的方法method(方法名可能匹配,参数类型不匹配,故还要分析参数类型)利用method.invoke方法Spring已经为此实现了完整的机制,......
  • springboot之json/yml配置文件的读取
    配置文件读取项目根目录的config目录下person.yml,文件夹如下person:name:qinjiangage:3happy:falsebirth:2000/01/01maps:{k1:v1,k2:v2}lis......
  • springboot集成nacos 配置中心
    nacos本机需要安装好,未安装时,参考创建一个springbootmyapi项目,使用maven进行依赖包管理,创建两个模块nacosconfig(配置中心)、nacosregister,(注册中心),本方主......
  • 230115_50_SpringBoot入门
    如果类中属性比较多,通过@value赋值比较麻烦。可以通过yaml配置文件给实例赋值。新建Person类,通过@ConfigurationProperties注解可以实现配置文件注入,其中prefix可以指......
  • springboot url中获取所有RequestMapping的URL路径列表集
    springboot项目在做URL权限控制的时候需要获取全部的URL,一个一个去controller中找费时费力,有的权限点的命名和URL有一定的对应关系。如果能用程序获得全部URL,将会省去很......
  • Spring Boot---(16)Spring Boot使用Thymeleaf开发web页面
    摘要:Spring官方不推荐使用JSP来开发WEB,而是推荐使用如下几种模板引擎来开发:Thymeleaf(Spring官方推荐)FreeMarkerVelocityGroovyMustache这里以Thymeleaf为例,介绍如何和Spri......
  • Spring Boot---(11)SpringBoot使用Junit单元测试
    摘要:本文详细的记录了SpringBoot如何结合Junit写测试用例,如何执行,打包执行,忽略执行等操作,SpringBoot内置了Junit测试组件,使用很方便,不用再单独引入其他测试组件。 演示环境......
  • SpringBoot完成SSM整合之SpringBoot整合junit
    SpringBoot​​......
  • springboot2.3.x版本发生异常时,响应的message和exception为空问题
    原因:因为boot2.3.x版本可能考虑信息安全问题,把以下两个值默认为server:error:include-message:neverinclude-exception:false发生异常是返回{"ti......