1. 程式人生 > >springboot 整合thymeleaf

springboot 整合thymeleaf

直接上程式碼...

1.maven引用

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.xichuan.dev</groupId>
	<artifactId>xichuan-thymeleaf</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>xichuan-thymeleaf</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.6.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
			<version>3.5</version>
		</dependency>

	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

2.application.yml

server:
  port: 8081

spring:
  application:
    name: xichuan-test
  thymeleaf:
    cache: false
    suffix: .html
    encoding: UTF-8
    prefix: classpath:/templates/
    check-template-location: true
    content-type: text/html
    mode: HTML5

3.在resources目錄下建立templates目錄,並建立first.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"></meta>
    <title>Insert title here</title>
</head>
<body>
<h4>this is the first page!,i am ok</h4>
</body>
</html>

4.建立介面

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

/**
 * Created by xichuan on 2018-11-28.
 */
@Controller
@RequestMapping("/web")
public class WebController {

    @RequestMapping("/first")
    public String first(){
        return "first";
    }

}

先install後,執行,訪問http://localhost:8081/web/first,即可得到下面頁面

 

-------------------------------額外新增----------------------------------------

上面first.html頁面中,使用<meta charset="UTF-8"></meta>,而不是<meta charset="UTF-8">。是因為mode: HTML5 會對HTML格式有嚴格的驗證。如果要取消此嚴格的格式要求,可以使用mode: LEGACYHTML5,需要新增下面額外jar包

		<dependency>
			<groupId>net.sourceforge.nekohtml</groupId>
			<artifactId>nekohtml</artifactId>
			<version>1.9.22</version>
		</dependency>

修改application.yml

spring:
  application:
    name: xichuan-test
  thymeleaf:
    cache: false
    suffix: .html
    encoding: UTF-8
    prefix: classpath:/templates/
    check-template-location: true
    content-type: text/html
    #mode: HTML5
    mode: LEGACYHTML5

修改頁面:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>First Page</title>
</head>
<body>
<h4>this is the first page!,i am ok</h4>
</body>
</html>

訪問http://localhost:8081/web/first依然可以訪問,並沒有報錯