• SpringBoot: 9.整合thymeleaf(转)


     

    1、创建maven项目,添加项目所需依赖

    复制代码
    <!--springboot项目依赖的父项目-->
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.0.RELEASE</version>
        </parent>
    
        <dependencies>
            <!--注入springboot启动器-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <!--注入springboot对thymeleaf视图技术的支持-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-thymeleaf</artifactId>
            </dependency>
        </dependencies>
    复制代码

    2、创建controller

    复制代码
    package com.bjsxt.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    /**
     * Created by Administrator on 2019/2/8.
     */
    @Controller
    public class IndexController {
    
        @RequestMapping("/toIndex")
        public String toIndex(Model model){
            model.addAttribute("msg","index页面");
            return "index";
        }
    }
    复制代码

    3、创建thymeleaf模版文件index.html

    目录位置:src/main/resources/templates
    templates:该目录是安全的。意味着该目录下的内容是不允许外界直接访问的。

    复制代码
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>thymeleaf</title>
    </head>
    <body>
        <span th:text="${msg}"></span>
        <hr>
        <span th:text="hello"></span>
    </body>
    </html>
    复制代码

    4、创建启动器,启动在浏览器中访问

    复制代码
    package com.bjsxt;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    /**
     * Created by Administrator on 2019/2/8.
     */
    @SpringBootApplication
    public class App {
    
        public static void main(String[] args){
            SpringApplication.run(App.class,args);
        }
    }
    复制代码

    目录结构

  • 相关阅读:
    DJANGO入门系列之(模型层:跨表操作)
    DJANGO入门系列之(模型层:单表操作)
    DJANGO入门系列之(模板层)
    DJANGO入门系列之(视图层)
    DJANGO入门系列之(模板层的简单介绍和视图层的扫尾)
    DJANGO入门系列之(虚拟环境的配置与安装)
    Django入门系列之(视图层基本概念)
    DJANGO入门系列之(路由控制)
    DJANGO入门系列之(Django请求生命周期and路由层)
    orm
  • 原文地址:https://www.cnblogs.com/kuangzhisen/p/10427164.html
Copyright © 2020-2023  润新知