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

springboot整合thymeleaf

  1. thymeleaf是對html進行渲染的一種技術。
  2. src/main/resources/templates是目錄安全的,不能通過外界直接訪問。
  • 建立springboot專案,修改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/xsd/maven-4.0.0.xsd">
    	<modelVersion>4.0.0</modelVersion>
    	<parent>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-parent</artifactId>
    		<version>1.5.8.RELEASE</version>
    	</parent>
    	<groupId>com.test</groupId>
    	<artifactId>spring-boot-freemarker-04</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
    	<!-- 修改jdk的版本 -->
    	<properties>
    		<java.version>1.8</java.version>
    		
    	</properties>
    	<!-- springboot啟動類座標 -->
    	<dependencies>
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-web</artifactId>
    		</dependency>
    		<!-- 新增thymeleaf的啟動類 -->
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-thymeleaf</artifactId>
    		</dependency>
    	</dependencies>
    </project>
  • 建立controller

    package com.test.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    /**
     * thymeleaf的入門案列
     * @author 26920
     *
     */
    @Controller
    public class DemoController {
    	@RequestMapping("/hello")
    	public String hello(Model model){
    		model.addAttribute("msg", "hello world");
    		return "index";
    	}
    }
    
  • 建立html檢視

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    	<span th:text="hello"></span>
    	<hr/>
    	<span th:text="${msg}"></span>
    </body>
    </html>
  • 建立啟動類,啟動專案,進行訪問

  • 直接訪問會有一個異常

  • 解決方案是:Thymeleaf.jar:更新為 3.0 以上的版本, thymeleaf-layout-dialect.jar:更新為 2.0 以上的版本,修改pom檔案

    <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>
    	<parent>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-parent</artifactId>
    		<version>1.5.8.RELEASE</version>
    	</parent>
    	<groupId>com.test</groupId>
    	<artifactId>spring-boot-freemarker-04</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
    	<!-- 修改jdk的版本 -->
    	<properties>
    		<java.version>1.8</java.version>
    		<!--修改thymeleaf的版本 -->
    		<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
    		<thymeleaf-layout-dialect.version>2.0.4</thymeleaf-layout-dialect.version>
    	</properties>
    	<!-- springboot啟動類座標 -->
    	<dependencies>
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-web</artifactId>
    		</dependency>
    		<!-- 新增thymeleaf的啟動類 -->
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-thymeleaf</artifactId>
    		</dependency>
    	</dependencies>
    </project>

    添加了thymeleaf的版本控制

  • 啟動專案訪問就可以看到頁面效果了