Intégration SpringBoot-freemarker, intégration thymeleaf

intégration freemarker

Ajouter une dépendance au fichier pom.xml

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

La structure du projet et la configuration application.yml sont les suivantes

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>

Démarrez le projet et testez comme suit:

 

intégration thymeleaf

La structure du projet est la suivante

Introduire des dépendances

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

placement 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";
	}

}

Les résultats des tests sont les suivants:

 

Ce qui précède est l'utilisation de base de freemarker et de thymeleaf, l'exemple de code peut être consulté dans les articles du répertoire du blog des technologies courantes de développement Spring Boot .

Je suppose que tu aimes

Origine blog.csdn.net/qq_33369215/article/details/89416299
conseillé
Classement