zoukankan      html  css  js  c++  java
  • Spring boot之使用thymeleaf

    操作步骤

    (1)在pom.xml中引入thymeleaf;

    (2)如何关闭thymeleaf缓存

    (3)编写模板文件.html

    (4)编写访问模板文件controller

    在pom.xml中引入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
    spring.thymeleaf.cache=false

    编写模板文件.html

    编写模板文件src/main/resouces/templates/hello.html:

    <!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 World!</title>
    </head>
    <body>
    <h1 th:inline="text">Hello.v.2</h1>
    <p th:text="${hello}"></p>
    </body>
    </html>

    编写访问模板文件controller

    @Controller
    public class TemplateController {

    /**
    * 返回html模板.
    */
    @RequestMapping("/helloHtml")
    public String helloHtml(Map<String,Object> map){
    map.put("hello","from TemplateController.helloHtml");
    return "/helloHtml";
    }

    }

    代码如下:

    模板

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8" />
    <title>Insert title here</title>
    </head>
    <body>
         <h1>
             Hello,thymeleaf
             <br />
             This is my first thymeleaf demo.
             
             
             <hr />
             
             welcome <span th:text="${name}"></span>
             
         </h1>
    </body>
    </html>

    配置文件

    ########################################################
    ###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
    #u5f00u53d1u8fc7u7a0bu5efau8baeu5173u95edu7f13u5b58.
    spring.thymeleaf.cache=false 

    controller类

    package com.kfit;
    
    import java.util.Map;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    /**
     * 注意:
     * 1.在Thymeleaf 模板文件中,标签是需要闭合的,3.0之前是需要闭合的
     * 2. thymeleaf 3.0+ 是可以不强制要求闭合的。
     * 
     * 3. 支持同时使用多个模板引擎,比如:thymeleaf和freemarker 可以并存。
     * 
     
     */
    @Controller
    @RequestMapping("/templates")
    public class TemplatesController {
        
        /**
         * 映射地址是:/templates/hello
         * @return
         */
        @RequestMapping("/hello")
        public String hello(Map<String,Object> map){
            //返回的是ModelAndView对象;
    //        ModelAndView mv = new ModelAndView("hello");
    //        return mv;
            map.put("name","Andy");
            return "hello";
        }
        
        @RequestMapping("/helloFtl")
        public String helloFtl(Map<String,Object> map){
            map.put("name","Andy");
            return "helloFtl";
        }
        
    }
  • 相关阅读:
    Ubuntu18.04+CUDA+CUDNN+Pytorch环境配置
    VIM入门必读(转)
    简述C和C++的学习历程(转)
    队列
    排序实现
    十进制转二进制
    北邮1467二叉树题引起的思考
    计算机是如何启动的?
    c语言字符串操作实现
    北邮机试——huffman权值问题
  • 原文地址:https://www.cnblogs.com/caoyingjielxq/p/9316808.html
Copyright © 2011-2022 走看看