IDEA SpringBoot +thymeleaf配置


1、pom添加以下依賴

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

之后更新maven

 

2、在application.properties中可以配置thymeleaf模板解析器屬性

#thymeleaf start
spring.thymeleaf.mode=HTML5 spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.content-type=text/html #開發時關閉緩存,不然沒法看到實時頁面 spring.thymeleaf.cache=false
#static 文件夾下的靜態文件訪問路徑
spring.mvc.static-path-pattern=/**
#thymeleaf end

3、編寫DEMO

 控制器

package com.example.demo;

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

@Controller
@RequestMapping(value = "/hello")
public class HelloController {
@RequestMapping(value = "/index")
public String hello(Model model) {
model.addAttribute("name", "Dear");
return "hello";
}
}

view

在resources下的templates下新建hello.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h2 data-th-text="${name}"></h2>
</body>
</html>

地址中輸入http://localhost:8080/hello/index

結果:

配置成功!


免責聲明!

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



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