SpringBoot集成Thymeleaf前端模版


1、在application.properties配置文件中添加 thymeleaf 的配置信息

spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root

spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.cache=false

2、在pom.xml中引入thymeleaf 的jar包

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

3、創建PageController並添加index方法

package com.cppdy.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class PageController {
    
    @RequestMapping("index")
    public String index(Model model) {
        model.addAttribute("name","吹泡泡的魚");
        return "index";
    }

}

4、在src/main/resources下創建templates(默認訪問此文件下的html),並創建index.html

   在src/main/resources下創建static(默認訪問此文件夾下的靜態文件),在static文件夾創建images文件夾,並放入一張圖片

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<title>SpringBoot</title>
</head>
<body>
    Welcome to cppdy
    <p th:text="${name}"></p>
    <img alt="吹泡泡的魚" th:src="@{/images/logo.jpg}"/>
    <br/>
    <input th:value="${name}"/>
</body>
</html>

注:@表示當前項目路徑,static文件夾是系統默認加載的靜態文件路徑


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM