springboot 模板 的两种实现方式

本文介绍了SpringBoot中Thymeleaf和Freemarker两种模板引擎的使用方法。Thymeleaf以HTML为基础,易于阅读,而Freemarker则需要配置模板存放路径。在Thymeleaf中展示了字符串和日期的处理,在Freemarker中提到了全局变量的使用注意事项。两种模板引擎各有特点,可根据项目需求选择。
摘要由CSDN通过智能技术生成

springboot模板的两种方式


在上一篇的博文中我们以经入门springboot了,在springboot中我们将告别以前我们的写前端的方式jsp。有的只是HTML和ftl页面,分别对应两种模板

Thymeleaf模板

关于Thymeleaf的优点,我只说一条:它就是html页面。
导入pom依赖:

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

Spring Boot官方文档建议在开发时将缓存关闭,那就在application.properties文件中加入下面这行

//正式环境还是要将缓存开启的
spring.thymeleaf.cache=false

对应的后台代码

实体类

package com.hu.springboot01.entity;

import lombok.Data;

@Data
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() {
   
    }
}

controller层代码:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>list</title>
</head>
<body>
springboot初步使用thymeleaf


<h2>显示文本</h2>
<span th:text="${name}"></span>

<hr>
<h2>显示HTML</h2>
<div th:utext="${msg}"></div>

<hr>
<h2>循环</h2>
<table>
    <tr>
        <td>用户的ID</td>
        <td>用户名</td>
        <td>密码</td>
    </tr>
    <tr th:each="u :${userList}">
        <td th:text="${u.uid}"></td>
        <td th:text="${u.userName}"></td>
        <td th:text="${u.password}"></td>
    </tr>
</table>

<hr style="color: crimson">
<h2>综合使用</h2>
<li th:each="u : ${userList}">
    <a href="#" th:title="${u.uid }" th:text="${#strings.length(u.userName) < 4 ? u.uid : #strings.substring(u.password, 0, 16)+'...' }">热点新闻1</a>
</li>

<h2>如何在页面定义变量</h2>

<div th:include="role/common/head2"></
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值