zoukankan      html  css  js  c++  java
  • Spring boot Thymeleaf 配置

    第一步: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配置
        <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>
    第五步:浏览器输入 看效果




  • 相关阅读:
    推荐体系算法总结
    Springboot 多模块调用,找不到注入的类
    LRU算法
    在SQLServer中连接另一个SQLServer库数据,在Oracle中连接另一个Oracle库数据,在SQL Server中连接Oracle数据,在Oracle中连接SQL Server数据
    用C#实现木马程序
    CSS filter 滤镜可视化配置
    微信小程序农历日期选择器 lunarpicker
    ms Sql 数据库出现 “提供的统计信息流已损坏”的解决办法。
    自制《要塞:十字军东征》无限金钱修改器
    c#+Winform实现自定义的“复制、粘贴”右键快捷菜单,多个控件共享使用一个右键菜单。
  • 原文地址:https://www.cnblogs.com/eason-d/p/7562831.html
Copyright © 2011-2022 走看看