springboot 整合thymeleaf

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zc_ad/article/details/84588949

直接上代码...

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依然可以访问,并没有报错

猜你喜欢

转载自blog.csdn.net/zc_ad/article/details/84588949
今日推荐