天天看點

SpringBoot學習06--整合Thymeleaf

接上一篇 SpringBoot學習05–使用MyBatis-Plus代碼生成器,在其基礎上整合

thymeleaf

1. 添加

thymeleaf

依賴

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
           

完整的pom.xml檔案如下:

<?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>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.6.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>demo</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<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-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-boot-starter</artifactId>
			<version>3.1.2</version>
		</dependency>
		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-generator</artifactId>
			<version>3.1.2</version>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid-spring-boot-starter</artifactId>
			<version>1.1.10</version>
		</dependency>
		<!--mybatis-plus完成項目建構所需模闆,真實項目不需要使用-->
		<dependency>
			<groupId>org.freemarker</groupId>
			<artifactId>freemarker</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

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

</project>
           

2.

application.yml

中添加相應

thymeleaf

配置

完整的

application.yml

如下:

# datasource config
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/demo?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8
    username: root
    password: admin123
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
  thymeleaf:
    mode: HTML
    encoding: UTF-8
    prefix: classpath:/template/
    suffix: .html
    servlet:
      content-type: text/html
    cache: false

# mybatis config
mybatis-plus:
  mapper-locations: classpath:mappers/*.xml
  type-aliases-package: com.example.demo.model
  global-config:
    db-config:
      id-type: auto
           

3. 在 resources目錄下建立 template目錄存放頁面

SpringBoot學習06--整合Thymeleaf

如上圖,在

template

下面建立一個

account.html

頁面

account.html

内容如下:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" 
xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
賬号資訊
<h3 th:text="${account.name}"></h3>
</body>
</html>
           

4. 添加 AccountController 代碼

package com.example.demo.controller;


import com.example.demo.entity.Account;
import com.example.demo.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zorroxu
 * @since 2019-07-05
 */
@Controller
@RequestMapping("/account")
public class AccountController {

    @Autowired
    private AccountService accountService;

    @GetMapping("/{id}")
    public String getAccount(@PathVariable("id") Long id, Model model){
        Account account = accountService.getById(id);
        model.addAttribute("account",account);
        return "account";
    }
}
           

5. 測試

DemoApplication.java

代碼如下

package com.example.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan(basePackages = "com.example.demo.dao")
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

}
           

啟動,可以看到控制台輸出如下

SpringBoot學習06--整合Thymeleaf

從上面可以看出,Tomcat啟動成功,端口使用的是預設的

8080

端口,如果想修改這個預設端口,可以在

application.yml

中添加如下資訊:

server:
  port: 9990  #這裡改成9990,你可以改成自己想要的端口
           

現在來測試頁面是否正确輸出

在浏覽器中輸入

http://localhost:8080/account/1

,可以看到頁面顯示如下:

SpringBoot學習06--整合Thymeleaf

至此,整合完成。

繼續閱讀