Springboot模板引擎

springboot之thymeleaf模板

在开发前建议把缓存关闭
在配置文件application.properties文件修改(我改为了yml文件)

spring.thymeleaf.cache=false

在这里插入图片描述
正式环境还是要将缓存开启的

Thymeleaf相当于html页面

相关pom依赖
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

在这里插入图片描述
在这里插入图片描述
使用ModelAndView
在这里插入图片描述

模板语法

页面加入<html xmlns:th="http://www.thymeleaf.org">
controller层UserController

package com.hsl.springboot01.controller;

import com.hsl.springboot01.entity.User;
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;

/**
 * @author water
 * @site www.water.com
 * @company xxx公司
 * @create 2019-12-28 16:45
 * <p>
 * thymeleaf的应用
 */
@Controller
@RequestMapping("/thymeleaf")
public class UserController {

    @RequestMapping("/users1")
    public String users1() {
        System.out.println("且歌且行且牵挂");
        return "users1";
    }

    @RequestMapping("/users2")
    public ModelAndView users2() {
        ModelAndView mv = new ModelAndView();
        mv.addObject("title","用户列表");
        List list=new ArrayList();
        list.add(new User(1,"water1","女",19));
        list.add(new User(2,"water2","女",19));
        list.add(new User(3,"water3","女",19));
        mv.addObject("users",list);
        //html转义
        mv.addObject("msg","<span style='color:red'> 优秀员工师water1</span>");
        mv.setViewName("users1");

//        System.out.println("隔花隔木隔慧之");
        return mv;
    }
}

html页面:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>thymeleaf应用</title>
</head>
<body>
<!--thymeleaf应用-->
<h1>thymeleaf语法</h1>
<!--<h3 th:text="${title}"></h3>-->

<table border="1" cellpadding="0" cellspacing="0" width="600px" style="text-align: center" align="center">
    <caption th:text="${title}"></caption>
    <tr>
        <th>员工ID</th>
        <th>员工姓名</th>
        <th>员工性别</th>
        <th>员工年龄</th>
    </tr>
    <tr th:each="u : ${users}">
        <td th:text="${u.uid}"></td>
        <td th:text="${u.uname}"></td>
        <td th:text="${u.sex}"></td>
        <td th:text="${u.age}"></td>
    </tr>
</table>
<p th:utext="${msg}"></p>
</body>
</html>

在这里插入图片描述

thymeleaf包含公共区域的方案

创建common.html

<div th:fragment="h1">
人生似水岂无涯
</div>
<div th:fragment="h2">
    浮云吹作雪
</div>
<div th:fragment="h3">
   世味煮成茶
</div>

页面添加代码:

<!--thymeleaf包含公共区域的方案-->
包整体
<div th:include="common.html"></div>
<hr>
包三部分
<div th:replace="common::h3"></div>
<hr>
包二三部分
<div th:replace="common::h1"></div>
<div th:replace="common::h3"></div>

在这里插入图片描述

springboot之freemarker模板

配置ftl文件

一开始我们新建freemarker.ftl文件师是找不到的
需要在setting配置一下
在这里插入图片描述
在这里插入图片描述
当然我们也可以不配置,只要创建html5文件,把后缀名改成ftl就可以了

应用

导入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>

yml文件配置

server:
  port: 80
  servlet:
    context-path: /

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


创建ftl文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>freemarker应用</title>
</head>
<body>
<h2>取值</h2>
<h3>提供默认值</h3>
<#--避免出现日常-->
welcome 【${name!'未知'}】 to freemarker!

<h3>exists用在逻辑判断</h3>
<#if name?exists>
    ${name}
</#if>

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

<h2>循环</h2>
<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.rname}</td>
        <td>${role.rtype}</td>
    </tr>
    </#list>
    </tbody>
</table>

<h2>include</h2>
<#include 'foot.ftl'>

<h2>局部变量(assign)/全局变量(global)</h2>
<#--获取项目名-->
 <#assign ctx1>
     ${springMacroRequestContext.contextPath}
 </#assign>

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

${ctx1}${ctx2}

</body>
</html>

controller层RoleController

package com.hsl.springboot01.controller;

import com.hsl.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;

/**
 * @author water
 * @site www.water.com
 * @company xxx公司
 * @create 2019-12-28 20:23
 *
 * freemarker应用
 */
@Controller
@RequestMapping("/freemarker")
public class RoleController {
    @RequestMapping("/roles")
    public ModelAndView roles() {
        ModelAndView mv = new ModelAndView();
        List list=new ArrayList();
        mv.addObject("name","一叶之秋");
        mv.addObject("sex","gay");

        list.add(new Role(1,"君莫笑","散人"));
        list.add(new Role(2,"包子入侵","流氓"));
        list.add(new Role(3,"沐雨橙风","枪炮师"));
        mv.addObject("roles",list);
        mv.setViewName("role");
        return mv;
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值