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>


====================打个广告,欢迎关注====================

QQ:412425870
csdn博客:
http://blog.csdn.net/caychen
码云:
https://gitee.com/caychen/
github:
https://github.com/caychen

点击群号或者扫描二维码即可加入QQ群:

328243383(1群)



点击群号或者扫描二维码即可加入QQ群:

180479701(2群)



  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值