1. 程式人生 > >SpringBoot使用模板引擎

SpringBoot使用模板引擎

springboot使用thymeleaf:

1、加入spring-boot-starter-thymeleaf依賴
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

2、在配置檔案中關閉thymeleaf快取
//開發過程中建議關閉cache
spring.thymeleaf.cache=false 


3、新建模板html,路徑放在src/main/resources/templates/下


4、新建模板controller類,使用@Controller註解,返回String或者ModelAndView值,該值指向templates目錄下檔案的路徑
@Controller
@RequestMapping("/thymeleaf")
public class ThymeleafController {

	@RequestMapping("/hello0")
	public String hello0(){
		//ModelAndView
		return "thymeleaf";
	}
	
	@RequestMapping("/hello1")
	public String hello1(Map<String, Object> map){
		//ModelAndView
		map.put("name", "Cay");
		return "thymeleaf";
	}
	
	@RequestMapping("/hello2")
	public ModelAndView hello2(){
		ModelAndView mv = new ModelAndView();
		mv.addObject("name", "Amy");
		mv.setViewName("thymeleaf");
		return mv;
	}
}

----------------------------------------------------
在spring-boot中使用freemarker
1、加入spring-boot-starter-freemarker依賴
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

2、在配置檔案中關閉freemarker快取
spring.freemarker.cache=false

3、新建模板ftl,路徑放在src/main/resources/templates/下


4、新建模板controller類,使用@Controller註解,返回String或者ModelAndView值,該值指向templates目錄下檔案的路徑
@Controller
@RequestMapping("/freemarker")
public class FreemarkerController {

	@RequestMapping("/hello0")
	public String hello0(){
		//ModelAndView
		return "freemarker";
	}
	
	@RequestMapping("/hello1")
	public String hello1(Map<String, Object> map){
		//ModelAndView
		map.put("name", "Cay");
		return "freemarker";
	}
	
	@RequestMapping("/hello2")
	public ModelAndView hello2(){
		ModelAndView mv = new ModelAndView();
		mv.addObject("name", "Amy");
		mv.setViewName("freemarker");
		return mv;
	}
}

應用程式的application.properties/yml/yaml配置檔案

server:
  port: 8081 #修改預設的埠號,預設為8080
  context-path: /springboot #修改預設的contextPath,預設為/

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql:///springboot
    username: root
    password: admin
    #springboot支援c3p0,dbcp,如果需要使用其他的資料庫連線池,指定type
    # type: 其他的使用指定的dataSource, 比如druid
    
  jpa:
    database: MYSQL
    show-sql: true

    hibernate:
      ddl-auto: update
      naming:
        strategy: org.hibernate.cfg.ImprovedNamingStrategy
    properties:
      hibernate.dialect: org.hibernate.dialect.MySQL5Dialect

  thymeleaf:
    cache: false #開發過程中建議關閉cache
    #encoding: UTF-8 #thymeleaf編碼
    #suffix: .html # thymeleaf字尾
    #content-type: text/html #格式
    
  freemarker:
    cache: false
    #charset: UTF-8
    #suffix: .ftl
    #content-type: text/html

freemarker.ftl檔案

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Insert title here</title>
</head>
<body>
	<h1>Freemarker模板檔案</h1>
	<#if name??>
		Welcome ${name}
	<#else>
		Hello world!
	</#if>
</body>
</html> 

thymeleaf.html檔案

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<title>Insert title here</title>
</head>
<body>
	<h1>Thymeleaf模板檔案</h1>
	Welcome <span th:text="${name}"></span>
</body>
</html>

====================打個廣告,歡迎關注====================