zoukankan      html  css  js  c++  java
  • 框架 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 这个标签的

  • 相关阅读:
    Software Architecture软件架构(方法、模式与框架)纵横谈
    SOLID: OOP的五大原则(译)
    《第一行代码》14章cool weather酷欧天气 网络请求相关问题
    前后端数据交互利器--Protobuf
    树状数组基础
    endless 如何实现不停机重启 Go 程序?
    fasthttp:比net/http快十倍的Go框架(server 篇)
    Mysql MVCC机制
    Docker构建mysql主从
    浅析b站2021/7/13日晚服务崩溃问题
  • 原文地址:https://www.cnblogs.com/1446358788-qq/p/14088948.html
Copyright © 2011-2022 走看看