SpringBoot03 整合thymeleaf

一、配置类

package com.yhw.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.util.MimeType;

import java.nio.charset.Charset;

@ConfigurationProperties(prefix = "spring.thymeleaf")
public class ThymeleafProperties {
    private static final Charset DEFAULT_ENCODING = Charset.forName("UTF-8");

    private static final MimeType DEFAULT_CONTENT_TYPE = MimeType.valueOf("text/html");

    public static final String DEFAULT_PREFIX = "classpath:/templates/";//前缀

    public static final String DEFAULT_SUFFIX = ".html";//后缀
}

二、控制层

package com.yhw.controller;

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

import java.util.Map;

@Controller
public class HelloController {
	

	@RequestMapping("/success")
	public String success(Map<String, Object> map) {
		map.put("hello", "通过模板引擎跳转到success.html页面");
		return "success";
	}
	
}

三、success.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>成功!</h1>
<div  th:text="${hello}"></div>
</body>
</html>

三、依赖包

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

四、运行结果

五、总结 配置类根据正则匹配thymeleaf解析html后缀文件

经验分享 程序员 微信小程序 职场和发展