SpringBoot集成Thymeleaf

上一篇給你們介紹了springboot整合freemarker,這一片來繼續爲你們介紹一種模板thymeleaf。css

首先在項目中增添thymeleaf依賴spring-boot-starter-thymeleaf 同時爲了解決html嚴格校驗報錯的問題,增添依賴nekohtml pom文件代碼以下:html

<?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.dalaoyang</groupId>
	<artifactId>springboot_thymeleaf</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>springboot_thymeleaf</name>
	<description>springboot_thymeleaf</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.10.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-thymeleaf</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>net.sourceforge.nekohtml</groupId>
			<artifactId>nekohtml</artifactId>
			<version>1.9.15</version>
		</dependency>
	</dependencies>

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


</project>

複製代碼

controller代碼大體與freemarker相同,代碼以下:java

package com.dalaoyang.controller;

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

/**
 * @author dalaoyang
 * @Description
 * @project springboot_learn
 * @package com.dalaoyang.controller
 * @email 397600342@qq.com
 * @date 2018/3/14
 */
@Controller
public class TestController {

    @RequestMapping("/")
    public String testThymeleaf(ModelMap modelMap){
        modelMap.addAttribute("msg", "Hello dalaoyang , this is thymeleaf");
        return "thymeleaf";
    }
}
複製代碼

application.properties以下:git

##端口號
server.port=8888


##去除thymeleaf的html嚴格校驗
spring.thymeleaf.mode=LEGACYHTML5

#設定thymeleaf文件路徑 默認爲src/main/resources/templates
spring.thymeleaf.prefix=classpath:/templates/ 
#設定靜態文件路徑,js,css等
spring.mvc.static-path-pattern=/static/**
# 是否開啓模板緩存,默認true
# 建議在開發時關閉緩存,否則無法看到實時頁面
spring.thymeleaf.cache=false
# 模板編碼
spring.thymeleaf.encoding=UTF-8
複製代碼

html代碼以下web

<!DOCTYPE html>
<!--解決th報錯 -->
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>thymeleaf</title>
</head>
<body>
<h1 th:text="${msg}"></h1>
</body>
</html>
複製代碼

啓動項目,訪問http://localhost:8888/ 便可看到如下頁面,spring

源碼下載 :大老楊碼雲apache

相關文章
相關標籤/搜索