SpringBoot入门-14(springboot配置thymeleaf使用YML)

一 pom.xml

<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/maven-v4_0_0.xsd">
 
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.fs</groupId>
  <artifactId>springboot_thymeleaf_9_1</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>springboot_thymeleaf_9_1 Maven Webapp</name>
  <url>http://maven.apache.org</url>
 
 
 
    <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>1.5.3.RELEASE</version>
    </parent>
 
    <properties>
      <spring-boot-version>1.5.3.RELEASE</spring-boot-version>
      <junit-version>4.12</junit-version>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <java.version>1.7</java.version>
    </properties>
 
 
    <dependencies>
      <!-- Compile -->
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
      </dependency>
 
      <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit-version}</version>
        <scope>test</scope>
      </dependency>
 
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
      </dependency>
 
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
 
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-freemarker</artifactId>
      </dependency>
 
      <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <scope>test</scope>
      </dependency>
    </dependencies>
 
  <build>
    <finalName>springboot_thymeleaf_9_1</finalName>
 
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
 
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
 
    </plugins>
  </build>
</project>

二、SampleWebThymeleaf3Application.java

package com.fs;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication
public class SampleWebThymeleaf3Application {
 
 
	public static void main(String[] args) throws Exception {
		SpringApplication.run(SampleWebThymeleaf3Application.class, args);
	}
 
}

三、ThymeleafController.java

package com.fs.controller;
 
import java.util.Map;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
 
@Controller
@RequestMapping("/")
public class ThymeleafController {
	
	@RequestMapping("/index")
	public String index(Map<String, Object> map){
		map.put("user", "Tyrone");
		return "index";
	}
	
}

四、application.yml

spring:
  thymeleaf:
    cache: false
    prefix: classpath:/templates/
    suffix: .html
    encoding: UTF-8
    content-type: text/html
    mode: HTML5

五、index.html

<!DOCTYPE html>
<html  xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<title>Insert title here</title>
</head>
<body>
	<p>Hello!<span th:text="${user}"></span>!<br />welcom to Thymeleaf's world</p>
</body>
</html>
posted @ 2022-03-12 10:38  能量熊  阅读(776)  评论(0编辑  收藏  举报