随笔 - 502  文章 - 1 评论 - 6 阅读 - 37万
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

第一步:pom.xml加入依赖

     <!-- HTML templates-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

如果出现错误 如下..
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'viewResolver' defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration$Thymeleaf3Configuration$Thymeleaf3ViewResolverConfiguration': Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration$ThymeleafDefaultConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaultTemplateResolver' defined in class path resource [org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration$Thymeleaf3Configuration$DefaultTemplateResolverConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver]: Factory method 'defaultTemplateResolver' threw exception; nested exception is java.lang.NoSuchMethodError: org.thymeleaf.templateresolver.TemplateResolver.checkNotInitialized()V
  
我们应该修改 Properties配置
1
2
3
4
<properties>
      <thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
      <thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>
</properties>

  

第二步: 修改yml文件

spring:
  thymeleaf:
      mode: HTML5
      encoding: utf-8
      content-type: text/html
      cache: false

 

第三步:修改Controller

    @GetMapping("/hello")
    String test(HttpServletRequest request) {
        //逻辑处理
        request.setAttribute("name", "java");
        return "/hello";
    }

第四步:修改html 放在resource/templates/hello.html
复制代码
<!DOCTYPE html >
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Hello World!</title>
</head>
<body>
<span th:text="'Welcome to our application, ' + ${name} + '!'"></span>
</body>
</html>
复制代码

 

第五步:浏览器输入 看效果

 





posted on   1161588342  阅读(1386)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
点击右上角即可分享
微信分享提示