springboot學習七--整合freemarker、整合thymeleaf

一,整合freemarker
1,啓動類

@SpringBootApplication(scanBasePackages= {"com.*.controller","com.*.service"})
public class Application {
	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}
}

2,application.yml

spring:
  freemarker:
    allow-request-override: false
    cache: true
    check-template-location: true
    charset: utf-8
    content-type: text/html; charset=utf-8
    expose-request-attributes: false
    expose-session-attributes: false
    expose-spring-macro-helpers: false
    suffix: .ftl
    template-loader-path: ["/WEB-INF/templates","classpath:/templates"] #多個路徑

默認會在resources下的templates下尋找
3,controller

@Controller
@RequestMapping("freemarker")
public class FreeMarkerController {
	@RequestMapping("freemarker.do")
	public String freemarker(Model model) {
		model.addAttribute("name", "admin");
		return "show";
	}
}

4,show.ftl
在這裏插入圖片描述

<html>
<head>
<title>Insert title here</title>
</head>
<body>
	<h1>歡迎你 ${name }</h1>
</body>
</html>

5,pom添加整合freemarker依賴

 <!-- 整合freemark -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-freemarker</artifactId>
		</dependency>

二,整合thymeleaf
1,application.yml

spring:
  thymeleaf:
    cache: false  #關閉thymeleaf緩存,否則沒有實時畫面
    check-template-location: true #檢查模板是否存在,然後在呈現
    content-type: text/html; charset=utf-8
    enabled: true
    mode: LEGACYHTML5
    prefix: /WEB-INF/templates/
    suffix: .html

2,controller

@Controller
@RequestMapping("thymeleaf")
public class ThymeLeafController {
	@RequestMapping("index.do")
	public  String index(Model model) {
		model.addAttribute("name", "admin");
		return "thymeleaf";
	}
}

4,pom

<!-- 整合thymeleaf -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
	<groupId>net.sourceforge.nekohtml</groupId>
	<artifactId>nekohtml</artifactId>
</dependency>

5,thymeleaf.html 需要使用thymeleaf標籤

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	Hello<h1 th:text="${name}"> </h1>
</body>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章