springboot使用 Thymeleaf 和 Freemarker 模板引擎(二)

Thymeleaf模板

thymeleaf 跟 JSP 一样,就是运行之后,就得到纯 HTML了。 区别在与,不运行之前, thymeleaf 也是 纯 html …
所以 thymeleaf 不需要 服务端的支持,就能够被以 html 的方式打开,这样就方便前端人员独立设计与调试, jsp 就不行了, 不启动服务器 jsp 都没法运行出结果来。

相关pom依赖

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

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

spring:
  thymeleaf:
    cache: false

对应的后台代码

package com.cpc.springboot01.entity;

/**
 * @Description:
 * @Author: cpc
 * @Date: 2019-11-08 10:45
 * @Version: V1.0
 */
public class User {

    private Integer uid;
    private String userName;
    private String password;

    public User(Integer uid, String userName, String password) {
        this.uid = uid;
        this.userName = userName;
        this.password = password;
    }

    public User() {
    }

    public Integer getUid() {
        return uid;
    }

    public void setUid(Integer uid) {
        this.uid = uid;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}


/**
 * @Description: 学习 thymeleaf 的请求类
 * @Author: cpc
 * @Date: 2019-11-08 10:46
 * @Version: V1.0
 */
@Controller
public class ThymeleafController {
    @RequestMapping("/list")
    public ModelAndView index(){
        ModelAndView mav = new ModelAndView();
        List list = new ArrayList();
        list.add(new User(1,"张三","123"));
        list.add(new User(2,"李四","456"));
        mav.addObject("userList",list);
        mav.addObject("msg","<span style='color:red'>用户列表</span>");
        mav.addObject("name", "zs");
        mav.setViewName("list");
        return mav;
    }
}

前台页面
在这里插入图片描述

list.htmll

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>thymeleaf 指定点讲解</title>
    </head>
    <body>
        <h1>显示文本</h1>
        <span th:text="${name}"></span>

        <h1>显示html</h1>
        <div th:utext="${msg}"></div>

        <h1>循环</h1>
        <table border="1px" width="600px">
            <thead>
                <tr>
                    <td>用户id</td>
                    <td>用户名</td>
                    <td>年龄</td>
                </tr>
            </thead>
            <tbody>
                <tr th:each="user : ${userList}">
                    <td th:text="${user.uid}"></td>
                    <td th:text="${user.userName}"></td>
                    <td th:text="${user.password}"></td>
                </tr>
            </tbody>
        </table>
        <br>
        <select name="" id="">
            <option th:each="user:${userList}" th:value="user.uid" th:text="${user.userName}">我是默认值 </option>
        </select>

        <h2>包含页面</h2>
        <h3>包含页面所有内容</h3>
        <div th:include="role/common/head2"></div>
        <h3>包含页面指定内容</h3>
        <div th:include="role/common/head2 :: h1"></div>
    </body>
</html>

head2.html


    <div th:fragment="h1">
        第一部分内容
    </div>
    <div th:fragment="h2">
        第二部分内容
    </div>
    <div th:fragment="h3">
        第三部分内容
    </div>

浏览器访问结果
在这里插入图片描述
更多骚操作:https://how2j.cn/k/springboot/springboot-thymeleat/1735.html

Freemarker模板

学习网站:http://freemarker.foofun.cn/

导入pom依赖

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

<!--可以不加,但是做项目的时候可能会用-->
 <resources>
            <!--解决mybatis-generator-maven-plugin运行时没有将XxxMapper.xml文件放入target文件夹的问题-->
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <!--freemarker模板也读取需要注释标红地方-->
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <!--<include>*.properties</include>-->
                    <!--<include>*.xml</include>-->
                    <!--<include>*.yml</include>-->
                </includes>
            </resource>
        </resources>

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/**

后台代码

package com.cpc.springboot01.controller;

import com.cpc.springboot01.entity.Role;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author: cpc
 * @Date: 2019-11-08 11:11
 * @Version: V1.0
 */
@Controller
public class FreemarkerController {

    @RequestMapping("/role/list")
    public ModelAndView roleList(){
        ModelAndView mav = new ModelAndView();
        mav.addObject("name",null);
        mav.addObject("sex","gay");
        List list = new ArrayList();
        list.add(new Role(1,"老师","教书育人"));
        list.add(new Role(2,"学生","知识改变命运"));
        mav.addObject("roles",list);
        mav.setViewName("list");
        return mav;
    }


    @RequestMapping("toLogin")
    public String toLogin(){
        return "login";
    }
}

Role

public class Role {
    private Integer rid;
    private String roleName;
    private String desc;

    public Role(Integer rid, String roleName, String desc) {
        this.rid = rid;
        this.roleName = roleName;
        this.desc = desc;
    }

    public String getRoleName() {
        return roleName;
    }

    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public Integer getRid() {
        return rid;
    }

    public void setRid(Integer rid) {
        this.rid = rid;
    }
}

在这里插入图片描述
list.ftl

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>freemarker 模板引擎讲解</title>
</head>
<body>
    讲解 freemarker 知识点
    <h2>获取值</h2>
     welcome 【${name!'未知'}】 to freemarker!


    <h3>exists用在逻辑判断</h3>
    <#--如果 name 存在就显示值-->
    <#if name?exists>
        ${name}
    </#if>

    <h2>条件</h2>
    <#if sex=='girl'>
        女
    <#elseif sex=='boy'>
        男
    <#else>
        保密
    </#if>

    <h2>循环</h2>
    <table border="1" 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>

    <h2>包含页面</h2>
    <#include 'common/head.ftl'>

    <h2>获取项目名</h2>
    ${springMacroRequestContext.contextPath}

    <h2>获取页面定义变量</h2>
    <h3>全局变量</h3>
    <#--这是导入全局变量文件-->
    <#include 'common/global.ftl'>
    ${ctx}/我是全局变量

    <h3>局部变量</h3>
    <#assign ctx2>
        ${springMacroRequestContext.contextPath}/我是局部变量
    </#assign>
    ${ctx2}
</body>
</html>

head.ftl
在这里插入图片描述
global.ftl

<#global ctx>
    ${springMacroRequestContext.contextPath}
</#global>

运行页面展示效果:
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值