springboot的thymeleaf模板与freemarker模板的基础用法

springboot的thymeleaf模板与freemarker模板的基础用法
1、springboot之thymeleaf模板
优点:最明显的优点就是它是html页面。下面直接上代码
使用thymeleaf首先要给他导入相关pom依赖

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

注意:Spring Boot官方文档建议在开发时将缓存关闭,那就在application.properties文件中加入下面这行
spring.thymeleaf.cache=false
正式环境还是要将缓存开启的

下面是我写的一个demo,主要是介绍它的基础用法

<!DOCTYPE html>
<html lang="en">
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>用户列表</title>
</head>
<body>
<h1 th:text="${title}">默认值</h1>

<table border="1px">
    <thead>
        <tr>
            <td>用户ID</td>
            <td>用户名</td>
            <td>用户描述</td>
        </tr>
    </thead>
    <tbody>
        <tr th:each="user : ${users}">
            <td th:text="${user.uid}"></td>
            <td th:text="${user.userName}"></td>
            <td th:text="${user.desc}"></td>
        </tr>
    </tbody>
</table>

<select>
    <option th:each="user : ${users}" th:value="${user.uid}" th:text="${user.userName}" ></option>
</select>
</body>
</html>

2、springboot之freemarker模板
①、导入相关的pom依赖

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

②、application.yml文件的默认配置
spring:
thymeleaf:
cache: false
freemarker:
# 设置模板后缀名
suffix: .ftl
# 设置文档类型
content-type: text/html
# 设置页面编码格式
charset: UTF-8
# 设置页面缓存
cache: false
# 设置ftl文件路径,默认是/templates,为演示效果添加role
template-loader-path: classpath:/templates/role
mvc:
static-path-pattern: /static/**
③、demo

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>角色列表</title>
    <#include 'comment.ftl'>
</head>
<body>
<hl>取值</hl>
welcome 【${name!'未知'}】to page

<h1>非空判断</h1>
<#if name??>
    xxxxx
</#if>

<hl>条件表达式</hl>
<#if set=='boy'>
    男
<#elseif sex=='girl'>
    女
<#else>
保密
</#if>

<h1>循环</h1>
<table border="1px" width="600px">
    <thead>
    <tr>
        <td>角色ID</td>
        <td>角色名</td>
        <td>角色描述</td>
    </tr>
    </thead>
    <tbody>
    <#list roles as role>
        <tr>
            <td >${role.rid}</td>
            <td >${role.roleName}</td>
            <td >${role.desc}</td>
        </tr>
    </#list>
    </tbody>
</table>


<hl>获取项目名(设置局部变量及全局变量)</hl>
<#--jsp ${pageContext.request.contextPath}-->
<#--将项目名赋值给ctrl这个变量,这边的作用域在当前页面-->
<#assign ctx1>
    ${springMacroRequestContext.contextPath}
</#assign>
<#--将项目名赋值给ctrl这个变量,这边的作用域在当整个项目-->
<#global ctx2>
    ${springMacroRequestContext.contextPath}
</#global>
${ctx1},${ctx2}


<h1>包含include</h1>
<#include 'foot.ftl'>
</body>
</html>

④、项目中怎么引用全局变量、局部变量
你项目中需要引入的文件等、、、
在这里插入图片描述
然后在页面上直接引用即可
在这里插入图片描述
⑤、freemarker模板页面取值的时候会出现的坑
如果${name}的值为null,他就会报下面错, 为’'就不会报错,这种情况的解决办法就是在在${name}后面加上一个感叹号${name!}
!就是一个默认值,如果有值就显示你的没值就显示默认值
在这里插入图片描述

⑥、注意:springboot项目,所有的页面都不能够直接跳页面,都会经过后台;它都会经过springmvc的视图解析器
在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/lisacheni/article/details/87639912