zoukankan      html  css  js  c++  java
  • Spring Boot 初体验(9)使用thymeleaf

    步骤:在pom.xml中引入thymeleaf

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

    关于thymeleaf的相关配置

    ########################################################
    ###THYMELEAF (ThymeleafAutoConfiguration)
    ########################################################
    #spring.thymeleaf.prefix=classpath:/templates/
    #spring.thymeleaf.suffix=.html
    #spring.thymeleaf.mode=HTML5
    #spring.thymeleaf.encoding=UTF-8
    # ;charset=<encoding> is added
    #spring.thymeleaf.content-type=text/html 
    # set to false for hot refresh 开发时设置不用缓存,生产环境设置为true
    spring.thymeleaf.cache=false  

    开始使用:

    编写模板文件 :默认都放在  src/main/resouces/templates文件夹下

            例如:src/main/resouces/templates/hello.html

    示例代码: 

    package com.mt.controller;
    
    import java.util.Date;
    import java.util.Map;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    @RequestMapping("/thymeleaf")
    public class ThymeleafController {
        @RequestMapping("/hello")
        public String hello(Map<String, Object> map) {
            map.put("date", new Date());
            return "thymeleafDemo";
        }
    }
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
          xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
        <head>
            <title>Hello thymeleaf!</title>
        </head>
        <body>
            <h1 th:inline="text">Hello.v.2</h1>
            <p th:text="${date}"></p>
        </body>
    </html>

    一个走投无路的庄稼汉
  • 相关阅读:
    暑假第二十七测
    暑假第二十七测
    【真题解】牛宫
    【伪题解】牛宫
    最优贸易
    跳马问题
    求和问题
    【题解】山区建小学
    OpenStack之虚机冷迁移代码简析
    OpenStack之虚机热迁移代码解析
  • 原文地址:https://www.cnblogs.com/kuangyefeige/p/9892672.html
Copyright © 2011-2022 走看看