springboot学习---常用模版 thymeleaf 和 freemarker 的简单了解

本文介绍了SpringBoot中常用的两种模板引擎Thymeleaf和Freemarker。对于Thymeleaf,讲解了相关POM依赖配置、关闭缓存的方法以及在开发和正式环境的应用。在内容中提到了HTML页面、实体类和Controller层的实现。对于Freemarker,介绍了导入的POM依赖和默认的application.yml配置,以及页面和Controller层的简要说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

thymeleaf 

1> 相关pom依赖

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

注意:

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

2>修改配置文件application.yml


spring:
  thymeleaf:
    cache: false

3>代码展示

html页面

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title th:text="${title}"></title>
</head>
<body>
<h2 th:text="${welcome}"></h2>

<h2>数据展示</h2>
<table>
    <thead>
        <tr>
            <th>编号</th>
            <th>姓名</th>
            <th>性别</th>
        </tr>
    </thead>
    <tbody>
        <tr th:each =" user : ${users}">
            <th th:text="${user.getUid()}">编号</th>
            <th th:text="${user.getUsername()}">姓名</th>
            <th th:text="${user.getSex()}">性别</th>
        </tr>
    </tbody>
</table>

<h3>
    <select>
        <option th:each="user : ${users}" th:value="${user.getUid()}" th:text="${user.getUsername()}"></option>
    </select>
</h3>

</body>
</html>

实体类

public class User {

    private String uid;
    private String username;
    private String sex;

    public User() {
    }

    public User(String uid, String username, String sex) {
        this.uid = uid;
        this.username = username;
        this.sex = sex;
    }

    public String getUid() {
        return uid;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}

controller层


@Controller
public class UserController {

    @RequestMapping("/user")
    public ModelAndView user(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("user");
        modelAndView.addObject("title","用户页面");
        modelAndView.addObject("welcome","欢迎来到用户列表界面");

        List<User> list = new ArrayList<>();
        list.add(new User("1","wangwangzaijiao","girl"));
        list.add(new User("2","taozi","girl"));
        list.add(new User("3","wangwangzaijiaotaozi","buxiaode"));

        modelAndView.addObject("users",list);
        return modelAndView;
    }

}

 

效果图

 

freemarker

导入pom依赖

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


application.yml文件的默认配置

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

页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>取一个好听的名字</title>
</head>
<body>

<h2>取值</h2>
取一个好听的名字,叫(${name!})
<h2>exits非空判断</h2>
<#if name?exists>
    123
</#if>

<h2>if条件判断结构</h2>
<#if name=='好听'>
好听的名字
<#elseif name=='不好听'>
不好听
<#else >
名字未知
</#if>

<h2>循环</h2>
<table>
    <thead>
    <tr>
        <th>编号</th>
        <th>名称</th>
        <th>备注</th>
    </tr>
    </thead>
    <tbody>
    <#list roles as role>
    <tr>
        <th>${role.rid}</th>
        <th>${role.roleName}</th>
        <th>${role.remark}</th>
    </tr>
    </#list>
    </tbody>
</table>



<h2>全局变量,局部变量</h2>
<#assign ctx1>
    ${springMacroRequestContext.contextPath}
</#assign>
<#global ctx2>
    ${springMacroRequestContext.contextPath}
</#global>

${ctx1},
</br>
${ctx2}

<h2>include</h2>
<#include "head.ftl">

</body>
</html>

 

controller

@RequestMapping("/role/role")
    public ModelAndView role(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("role");
//"好听"
        modelAndView.addObject("name","好听");

        List<Role> list = new ArrayList<>();
        list.add(new Role("1","wangwangzaijiao","nizhidaoma"));
        list.add(new Role("2","taozi","buzhidaolei"));
        list.add(new Role("3","wangwangzaijiaotaozi","nawoyebuzhidaole"));

        modelAndView.addObject("roles",list);
        return modelAndView;
    }

role


public class Role {

    private String rid;
    private String roleName;
    private String remark;

    public Role() {
    }

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

    public String getRid() {
        return rid;
    }

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

    public String getRoleName() {
        return roleName;
    }

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

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值