SpringBoot-07引擎模板thymeleaf和freemarker的使用

在springboot中使用模板引擎的大致步骤:
 ① 在pom.xml中添加相应的依赖(thymeleaf或freemarker);
 ② 在application.properties中添加配置信息,开发中建议关闭缓存;
 ③ 编写模板文件, thymeleaf的默认后缀是.html; freemarker的默认后缀是.ftl(都可在application.properties中配置修改)
 ④ 编写访问模板文件的controller;

一. springboot使用thymeleaf模板引擎

总览:
 ① 在pom.xml中引入thymeleaf的依赖;
 ② 在application.propertiest中配置thymeleaf;
 ③ 在resource下创建模板文件夹templates; 并创建页面;
 ④ 编写controller, 返回视图名或ModelAndView对象;

步骤1:
在pom.xml中引入thymeleaf的依赖:

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

步骤2 :
在application.properties中配置thymeleaf:

# 是否开启thymeleaf缓存
spring.thymeleaf.cache=false
# 前缀
#spring.thymeleaf.prefix=classpath:/templates/
# 后缀
#spring.thymeleaf.suffix=.jsp
#
#spring.thymeleaf.model=HTML5
# 编码
#spring.thymeleaf.encoding=UTF8
#spring.thymeleaf.content-type=text/html

步骤3 :
在resource下创建模板文件夹templates, 并创建页面:
这里写图片描述
步骤4 :
编写controller, 返回视图名或ModelAndView对象:

情况一: 仅仅返回视图

package online.bendou.controller;

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

@Controller
@RequestMapping("/thymeleaf")
public class ThymeleafController {

    @RequestMapping("/")
    public String toIndex(){
        return "index";
    }
}

情况二: 返回视图和数据
在方法参数中添加Map<String, Object> map形参, 可以将值放在此map中可以携带回页面(具体是什么原理以后再说)

package online.bendou.controller;

import java.util.Map;

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

@Controller
@RequestMapping("/thymeleaf")
public class ThymeleafController {

    @RequestMapping("/")
    public String toIndex(Map<String,Object> map){
        map.put("name", "Lx");
        return "index";
    }

}

页面使用EL取值:

<h3>welcom, <span th:text="${name}" ></span></h3>

二. springboot使用freemarker模板引擎

步骤1 :
在pom.xml中引入thymeleaf的依赖:

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

步骤2 :
在application.properties中配置freemarker:

spring.freemarker.charset=UTF-8
spring.freemarker.cache=false
spring.freemarker.allow-request-override=false
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-session-attributes=false
spring.freemarker.expose-spring-macro-helpers=false
#spring.freemarker.prefix=
#spring.freemarker.request-context-attribute=
#spring.freemarker.settings.*=
#spring.freemarker.suffix=.ftl
#spring.freemarker.template-loader-path=classpath:/templates/
#spring.freemarker.view-names=

步骤3 :
在resource下创建模板文件夹templates, 并创建页面:
这里写图片描述
步骤4 :
编写controller, 返回视图名或ModelAndView对象:
情况一: 仅仅返回视图

package online.bendou.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/freemarker")
public class FreeMarkerController {

    @GetMapping("/hello")
    public String hello(){
        return "hello";
    }

}

情况二: 返回视图和数据
在方法参数中添加Map<String, Object> map形参, 可以将值放在此map中可以携带回页面(具体是什么原理以后说)

package online.bendou.controller;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/freemarker")
public class FreeMarkerController {

    @GetMapping("/hello")
    public String hello(Map<String, Object> map){
        map.put("name", "Lx");
        return "hello";
    }

}

页面使用EL取值:

<h2>Hello, ${name}</h2>

注:
 两个模板引擎( thymeleaf和freemarker )可以同时配置使用.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值