• 框架 Spring Boot 技术入门到整合 6-2 Springboot 整合thymeleaf


    0    课程网址

    https://www.imooc.com/video/16721

    1    配置文件
    1.1  pom.xml  引入依赖
            <!-- 引入 thymeleaf 模板依赖 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-thymeleaf</artifactId>
            </dependency>
    1.2  application.properties 引入thymeleaf相关配置
    ############################################################
    #
    # 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
    2    相关demo
    2.1  动态静态demo

    ThymeleafController

    package com.example.demo.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.ModelMap;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    @RequestMapping("th2")
    public class ThymeleafController {
    
        @RequestMapping("/index")
        public String index(ModelMap map) {
            //塞值name ,用于展示
            map.addAttribute("name", "thymeleaf-ddwei");
            return "thymeleaf/index";
        }
        
        @RequestMapping("center")
        public String center() {
            return "thymeleaf/center/center";
        }
        
    
    }

    center.html

    <!DOCTYPE html>
    <html>
    <head lang="en">
        <meta charset="UTF-8" />
        <title></title>
    </head>
    <body>
    Thymeleaf模板引擎
    <h1>center page</h1>
    </body>
    </html>

    index.html

    使用了el表达式,实现了动静分离,前端人员写出hellow world,后端人员塞值name就可以,塞完后,直接展示后端人员给的值(不再展示前端配置定的hello world)

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

    测试静态:

    测试动态:

    如果,<p th:text ="${name}">123</p> 后台 传过来 要是存在,他显示 name的 值,如果后台传过来没有 name 这个属性 他是不会走 p 这个标签的

  • 相关阅读:
    Vue-cli3中导入Cesium并配置
    Intellij IDEA中安装插件的两种方式
    Docker01——Ubuntu上安装Docker
    Java反射02——动态代理
    Java反射01——基本概念
    Docker安全配置问题
    马踏棋盘里面的一些小问题
    马踏棋盘算法用Java语言实现
    Android 中AIDL的使用与理解
    数据库技术中的触发器(Trigger)——和ContentObserver功能类似
  • 原文地址:https://www.cnblogs.com/1446358788-qq/p/14088948.html
Copyright © 2020-2023  润新知