1. 程式人生 > >SpringBoot學習9:springboot整合thymeleaf

SpringBoot學習9:springboot整合thymeleaf

tap str har create urn char index mod version

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);
    }
}

目錄結構

技術分享圖片

SpringBoot學習9:springboot整合thymeleaf