SpringBoot-freemarker整合、thymeleaf整合

freemarker整合

pom.xml文件添加依賴

<!-- 引入freemarker模板依賴 -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

項目結構和application.yml配置如下

application.yml

spring:
  # freemarker靜態資源配置
  freemarker: 
#    設定ftl文件路徑
    template-loader-path: classpath:/templates
#   關閉緩存,及時刷新,上線生產環境需要改爲true
    cache:  false
    charset: UTF-8
    check-template-location: true
    content-type: text/html
    expose-request-attributes: true
    expose-session-attributes: true
    request-context-attribute: request
    suffix: .ftl

FreemarkerController.java

package com.xiangty.controller;

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

import com.xiangty.pojo.Resource;

@Controller
@RequestMapping(value = "ftl")
public class FreemarkerController {

	@Autowired
	private Resource resource;

	@RequestMapping("/index")
	public String index(ModelMap map) {
		map.addAttribute("resource", resource);
		return "freemarker/index";
	}

	@RequestMapping("/center")
	public String center() {
		return "freemarker/center/center";
	}

}

templates/freemarker/index.ftl

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8" />
    <title>FreeMarker模板引擎</title>
</head>
<body>
FreeMarker模板引擎
<h1>${resource.name}</h1>
<h1>${resource.website}</h1>
<h1>${resource.language}</h1>
</body>
</html>

templates/freemarker/center/center.ftl

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8" />
    <title>FreeMarker模板引擎</title>
</head>
<body>
FreeMarker模板引擎
<h1>center page</h1>
</body>
</html>

啓動項目,測試如下:

 

thymeleaf整合

項目結構如下

引入依賴

<!-- 引入 thymeleaf 模板依賴 -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

application.yml配置

spring:
# thymeleaf靜態資源配置
  thymeleaf:
    #關閉緩存,及時刷新
    cache: false
    prefix: classpath:/templates/
    suffix: .html
    mode: HTML5
    encoding: UTF-8
    servlet:
      content-type: text/html

templates/thymeleaf/index.html

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8" />
    <title>Thymeleaf模板引擎</title>
</head>
<body>
Thymeleaf模板引擎
<h1 th:text="${name}">hello world~~~~~~~</h1>
</body>
</html>

ThymeleafContorller.java

package com.xiangty.controller;

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


@Controller
@RequestMapping(value = "th")
public class ThymeleafController {

	@RequestMapping("/index")
	public String index(ModelMap map) {
		map.addAttribute("name", "thymeleaf測試Name");
		return "thymeleaf/index";
	}

	@RequestMapping("/center")
	public String center() {
		return "thymeleaf/center/center";
	}

}

測試效果如下:

 

以上是freemarker、thymeleaf的基本用法,示例代碼可以查看Spring Boot開發常用技術博客目錄內的文章。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章