thymeleaf和freemarker的使用对比

一、thymeleaf

1、依赖

	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-thymeleaf</artifactId>
	</dependency>

2、配置

#thymeleaf
# 开发时关闭缓存,不然没法看到实时页面
spring.thymeleaf.cache=false
# 用非严格的 HTML
spring.thymeleaf.mode: HTML 
spring.thymeleaf.encoding: UTF-8
spring.thymeleaf.servlet.content-type: text/html

3、使用
(1-1)后台部分,接口写法:

import org.springframework.ui.Model;
@Controller
public class TestController {
	@RequestMapping("/test")
	public String test(Model model) {
		model.addAttribute("name","测试shiro的功能");
		return "test";
	}
	}

(1-2)如果处理结果需要重定向,则参数(org.springframework.ui.Model model)得换成(org.springframework.web.servlet.mvc.support.RedirectAttributes redirectAttributes)
在这里插入图片描述

(2)返回的静态html页面放在这里,如下图:在这里插入图片描述
(3)静态html页面写法
在这里插入图片描述
补充一些thymeleaf的语法:参考------Thymeleaf整合

1、 核心用法
th:xxx:动态渲染指定的 html 标签属性值、或者th指令(遍历、判断等)
● th:text:标签体内文本值渲染
  ○ th:utext:不会转义,显示为html原本的样子。
● th:属性:标签指定属性渲染
● th:attr:标签任意属性渲染
● th:ifth:each...:其他th指令
例如:
<p th:text="${content}">原内容</p>
<a th:href="${url}">登录</a>
<img src="../../images/gtvglogo.png" 
th:attr="src=@{/images/gtvglogo.png},title=#{logo},alt=#{logo}" />

2、表达式:用来动态取值
● ${}:变量取值;使用model共享给页面的值都直接用${}
● @{}:url路径;
● #{}:国际化消息
● ~{}:片段引用
● *{}:变量选择:需要配合th:object绑定对象

二、freemarker

1、引入依赖,

<dependency>
		    <groupId>org.springframework.boot</groupId>
		    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

2、配置。要在配置文件配置静态页面的地址,如html页面freemarker模板配置如下,可直接拷贝使用
spring.freemarker.templateLoaderPath=classpath:/templates/ spring.freemarker.allow-request-override=false spring.freemarker.cache=true spring.freemarker.check-template-location=true spring.freemarker.charset=UTF-8 spring.freemarker.content-type=text/html spring.freemarker.expose-request-attributes=false spring.freemarker.expose-session-attributes=false spring.freemarker.suffix=.html #spring.freemarker.templateEncoding=UTF-8 spring.freemarker.expose-spring-macro-helpers=false
3、使用。
(1)后台接口

@Controller
public class FreeMarkerController {
    @RequestMapping("/test")
    public String test1(Map<String,Object> map){
        //map放的是模板 需要使用的数据
        Student studentLS=new Student();
        studentLS.setName("李四");
        studentLS.setAge(17);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String date=sdf.format(new Date());
        studentLS.setBirthday(date);
        studentLS.setDate(new Date());
        studentLS.setMoney(98.02f);
        Student studentZS=new Student();
        studentZS.setName("张三");
        studentZS.setAge(18);
        studentZS.setBirthday(date);
        studentZS.setDate(new Date());
        studentZS.setMoney(97.01f);
        studentLS.setBestFriend(studentZS);
        List<Student> list=new ArrayList<Student>();
        list.add(studentLS);
        list.add(studentZS);
        studentZS.setFriends(list);
        studentLS.setFriends(list);
        studentZS.setBestFriend(studentLS);
        map.put("students",list);
        map.put("point","12356874");
        HashMap<String,Student> hashMap=new HashMap<>();
        hashMap.put("ZS",studentZS);
        hashMap.put("LS",studentLS);
        map.put("stuMap",hashMap);
        //返回基于resources/templates的路径的模板
        return "test";
    }

(2)位置和thymeleaf的页面位置一样
(3)前端ftl页面

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<table>
    <tr>
        <td>
            序号
        </td>
        <td>
            姓名
        </td>
        <td>
            年龄
        </td>
        <td>
            生日
        </td>
        <td>
            余额
        </td>
        <td>
            日期
        </td>
        <#--<td>-->
            <#--朋友列表-->
        <#--</td>-->
        <#--<td>-->
            <#--好友-->
        <#--</td>-->
    </tr>
    <#if students??>
    <#list students as student >
    <tr>
        <td>
            ${student_index}
        </td>
        <td <#if student.name=="张三">style="background-color: crimson" </#if>>
            ${student.name}
        </td>
        <td <#if student.age gt 10>style="background-color: darkturquoise" </#if>>
            ${student.age}
        </td>
        <td>
            ${student.birthday}
        </td>
        <td>
            ${student.money}
        </td>
        <td>
            ${student.date?string("YY年MM月dd日")}
        </td>
        <#--<td>-->
            <#--${student.friends}-->
        <#--</td>-->
        <#--<td>-->
            <#--${student.bestFriend}-->
        <#--</td>-->
    </tr>
    </#list>
    </#if>
    <br/>
    学生个数:${students?size}
</table>
<br/>
<br/>
姓名:${(stuMap['ZS'].name)!"无数据"}
<br/>
姓名:${(stuMap.LS.name)!"无数据"}
<br/>
<br/>
<#if stuMap??>
遍历stuMap:
<#list stuMap?keys as key>
    ${key},<br/>
</#list>
</#if>
<br/>
${point}
</br>
<#assign text="{'bank':'中国工商银行','account':'56565648418751548'}"/>
<#assign data=text?eval />
开户行:${data.bank}<br/>
卡号:${data.account}<br/>
</body>
</html>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值