zoukankan      html  css  js  c++  java
  • Spring Boot整合模板引擎thymeleaf

    项目结构

    引入依赖pom.xml

    <!-- 引入 thymeleaf 模板依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    

    配置application.properties

    ############################################################
    #
    # thymeleaf 静态资源配置
    #
    ############################################################
    spring.thymeleaf.prefix=classpath:/templates/
    spring.thymeleaf.suffix=.html
    spring.thymeleaf.mode=HTML5
    spring.thymeleaf.encoding=UTF-8
    # spring.thymeleaf.content-type=text/html
    # 关闭缓存, 即时刷新, 上线生产环境需要改为true
    spring.thymeleaf.cache=false
    

    模板页index.html

    <!DOCTYPE html>
    <html>
    <head lang="en">
        <meta charset="UTF-8" />
        <title></title>
    </head>
    <body>
    Thymeleaf模板引擎
    <h1 th:text="${name}">hello world~~~~~~~</h1>
    </body>
    </html>
    

    控制器Controller

    @Controller
    @RequestMapping("demo/th")
    public class ThymeleafController {
        @RequestMapping("/index")
        public String index(ModelMap map) {
            map.addAttribute("name", "thymeleaf-nick");
            return "thymeleaf/index";
        }
    
        @RequestMapping("center")
        public String center() {
            return "thymeleaf/center/center";
        }
    }
    

    测试

    完整代码

  • 相关阅读:
    206.反转链表
    gprof
    Java【Stream流、方法引用】学习笔记
    Java【函数式接口(Supplier、Comsumer、Predicate、Function)】学习笔记
    Python exec 内置语句
    Python sorted() 函数
    Python oct() 函数
    Python id() 函数
    Python dir() 函数
    软件测试的方法
  • 原文地址:https://www.cnblogs.com/okokabcd/p/9202025.html
Copyright © 2011-2022 走看看