Spring Boot中使用 Thymeleaf

1.pom.xml引入thymeleaf

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

2.關閉緩存application.properties

spring.thymeleaf.cache=false

3.編寫Controller類

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

import java.util.Map;

@Controller
@RequestMapping("/home")
public class HomeController {

    @RequestMapping(value = "/index")
    public String index(Map<String,Object> map) {
        map.put("userName","Bill Gates");
        return "index";
    }
}

4.模板html

/resources/templates/index.htmljava

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8"></meta>
    <title>Title</title>
</head>
<body>
<h1>hello,<span th:text="${userName}"></span></h1>
</body>
</html>

5.運行結果

訪問地址:http://localhost:8090/home/index
web

hello,Bill Gates

相關文章
相關標籤/搜索