41、springboot 整合 FreeMarker 模版技术

news/2024/10/25 3:28:42/

springboot 整合 FreeMarker 模版技术

★ 整合FreeMarker的自动配置:

FreeMarkerAutoConfiguration:负责整合Spring容器和获取FreeMarkerProperties加载的配置信息。FreeMarkerServletWebConfiguration/FreeMarkerReactiveWebConfiguration:整合FreeMarker的自动配置类。FreeMarkerProperties类则对应application.properties文件中关于FreeMarker的配置属性,它负责读取该文件并设置FreeMarker。

★ 添加整合FreeMarker的starter:

在pom.xml文件中导入spring-boot-starter-freemarker 
如要使用Bootstrap,则添加org.webjars:boostrap
如要使用版本无关的WebJar,则添加org.webjars:webjars-locator-core依赖 

配置文件配置 Freemarker 的属性
在这里插入图片描述

★ 页面变化

 FreeMarker需要在页面模板中添加自己的指令,而且表达式还要写在HTML元素中,例如下面代码
<div class=“alert alert-danger”>${error}</div>,${error}就写在了HTML的<div.../>元素内,这就对原有HTML页面形成了污染;Thymeleaf则只需为HTML标签中添加th:xxx属性,在模板被解析之前,这些属性会被浏览器直接忽略,
因此它不会对原有HTML页面形成污染。

代码演示:

拷贝上一份springboot整合 thymeleaf的代码,修改成 freemarker
springboot整合Thymeleaf
pom.xml 和 compiler.xml 文件里面需要把名字换成新的项目名,my_freemarker.iml 文件只需要修改文件名
在这里插入图片描述

1、修改依赖。,添加整合FreeMarker的starter

在这里插入图片描述

2、修改配置文件,整合freemarker 的一些配置属性

在这里插入图片描述

3、前端页面名字的后缀都改成 .ftlh

引入js文件的写法改变
在这里插入图片描述
index页面
在这里插入图片描述

main页面
在这里插入图片描述
books页面
在这里插入图片描述

效果也一样能出来:
在这里插入图片描述

代码:
index

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>登录页面</title><!--  引入css样式,用 link 元素  ,  stylesheet 样式单 , .gz表示是打包的,但是springboot会自动解包 --><!--  引入 Bootstrap 的 Web Jar 中的 CSS 样式  --><link rel="stylesheet" href="/webjars/bootstrap/css/bootstrap.min.css"><!--  jquery 放在 bootstrap 前面,因为 bootstrap 需要依赖到 jquery  --><!--  引入 jQuery 的 Web Jar 中的 js 脚本  --><script type="text/javascript" src="/webjars/jquery/jquery.min.js"></script><!--  引入 Bootstrap 的 Web Jar 中的 js 脚本  --><script type="text/javascript" src="/webjars/bootstrap/js/bootstrap.bundle.min.js"></script><!--  引入 popper 的 Web Jar 中的 Js 脚本  --><script type="text/javascript" src="/webjars/popper.js/umd/popper.min.js"></script></head>
<body>
<div class="container"><img src="/logo.jpg" width="100px" height="100px" class="rounded mx-auto d-block"><h4>用户登录</h4><#if error?exists><div class="alert alert-danger"}>${error}</div></#if><form method="post" action="/login"><div class="form-group row"><label for="username" class="col-sm-3 col-form-label">用户名:</label><div class="col-sm-9"><input type="text" id="username" name="username"class="form-control" placeholder="输入用户名"></div></div><div class="form-group row"><label for="password" class="col-sm-3 col-form-label">密码:</label><div class="col-sm-9"><input type="password" id="password" name="password"class="form-control" placeholder="输入密码"></div></div><div class="form-group row"><div class="col-sm-6 text-right"><button type="submit" class="btn btn-primary">登录</button></div><div class="col-sm-6"><button type="reset" class="btn btn-danger">重设</button></div></div></form>
</div>
</body>
</html>

main

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>首页</title><!--  引入css样式,用 link 元素  ,  stylesheet 样式单 , .gz表示是打包的,但是springboot会自动解包 --><!--  引入 Bootstrap 的 Web Jar 中的 CSS 样式  --><link rel="stylesheet" href="/webjars/bootstrap/css/bootstrap.min.css"><!--  jquery 放在 bootstrap 前面,因为 bootstrap 需要依赖到 jquery  --><!--  引入 jQuery 的 Web Jar 中的 js 脚本  --><script type="text/javascript" src="/webjars/jquery/jquery.min.js"></script><!--  引入 Bootstrap 的 Web Jar 中的 js 脚本  --><script type="text/javascript" src="/webjars/bootstrap/js/bootstrap.bundle.min.js"></script><!--  引入 popper 的 Web Jar 中的 Js 脚本  --><script type="text/javascript" src="/webjars/popper.js/umd/popper.min.js"></script></head>
<body>
<div class="container"><img src="/logo.jpg" width="100px" height="100px" class="rounded mx-auto d-block"><div class="text-info">您好,<span>${Session.username}</span></div><br>职位:<div><#--  springboot规定获取Session对象首字母要大写  --><#switch Session['role']><#case 'admin'><span>管理员</span><#break><#case 'manager'><span>项目经理</span><#break><#default><span>普通员工</span></#switch></div><br><br><a href="/viewBooks" class="btn btn-info">查看图书列表</a>
</div>
</body>
</html>

books

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>testBranch</title><!--  引入css样式,用 link 元素  ,  stylesheet 样式单 , .gz表示是打包的,但是springboot会自动解包 --><!--  引入 Bootstrap 的 Web Jar 中的 CSS 样式  --><link rel="stylesheet" href="/webjars/bootstrap/css/bootstrap.min.css"><!--  jquery 放在 bootstrap 前面,因为 bootstrap 需要依赖到 jquery  --><!--  引入 jQuery 的 Web Jar 中的 js 脚本  --><script type="text/javascript" src="/webjars/jquery/jquery.min.js"></script><!--  引入 Bootstrap 的 Web Jar 中的 js 脚本  --><script type="text/javascript" src="/webjars/bootstrap/js/bootstrap.bundle.min.js"></script><!--  引入 popper 的 Web Jar 中的 Js 脚本  --><script type="text/javascript" src="/webjars/popper.js/umd/popper.min.js"></script></head>
<body>
<div class="container"><img src="/logo.jpg" width="100px" height="100px" class="rounded mx-auto d-block"><table class="table table-hover"><tr><th>序号</th><th>Id</th><th>书名</th><th>价格</th></tr><#list books  as book><tr><td>${book_index}</td><td>${book.id}</td><td>${book.name}</td><td>${book.price}</td></tr></#list></table>
</div>
</body>
</html>

http://www.ppmy.cn/news/1076047.html

相关文章

Ansible学习笔记15

1、roles&#xff1a;&#xff08;难点&#xff09; roles介绍&#xff1a; roles&#xff08;角色&#xff09;&#xff1a;就是通过分别将variables&#xff0c;tasks及handlers等放置于单独的目录中&#xff0c;并可以便捷地调用他们的一种机制。 假设我们要写一个playbo…

『Newsletter 丨第二期』PieCloudDB Database 新增控制台、LDAP 支持、虚拟数仓日志等多项功能

PieCloudDB Database 最新动态 云上云版「控制台」功能上线 PieCloudDB 云上云版「控制台」功能全新上线&#xff0c;控制台集成了组织、数仓、用户、费用、权限等多方位管理功能&#xff0c;支持在一个组织下创建和管理多个数仓&#xff0c;并支持独立的一套权限管理体系。管…

package.json 详解

文章目录 package.json1. name2. version3. description4. homepage5. bugs6. license7. author, contributors8. funding9. files10. main11. module12. browser13. bin14. man15. directories15.1 directories.bin15.2 directories.man 16. repository17. scripts18. config1…

​放弃数据库,改用Kafka!

长期以来&#xff0c;数据库一直充当着记录系统&#xff0c;它们以可靠且持久的方式存储和管理关键数据&#xff0c;也赢得了大多数公司的信赖。 但时代在变。许多新兴趋势正在影响当今数据的存储和管理方式&#xff0c;不得不让一些技术决策者们重新考虑数据存储究竟还有哪些…

计算机存储容量单位读法及换算B、KB、MB、GB、TB、PB、EB、ZB、YB、RB、QB

1KB1024B&#xff1b;1MB1024KB10241024B。 1B&#xff08;byte&#xff0c;字节&#xff09; 8 bit&#xff08;位&#xff0c;比特&#xff09;&#xff1b; 1KB&#xff08;Kilobyte&#xff0c;千字节&#xff09;1024 B 2^10 B&#xff1b; 1MB&#xff08;Megabyte&am…

深度学习(前馈神经网络)知识点总结

用于个人知识点回顾&#xff0c;非详细教程 1.梯度下降 前向传播 特征输入—>线性函数—>激活函数—>输出 反向传播 根据损失函数反向传播&#xff0c;计算梯度更新参数 2.激活函数(activate function) 什么是激活函数&#xff1f; 在神经网络前向传播中&#x…

xml中in的使用

目录 一、简介 二、使用 1、参数为list 2、参数为Array 3、参数为Map XML中大于、小于、不等于符号使用 一、简介 在xml中使用in查询需要使用foreach标签 <foreach item"item" collection"list" index"index" open"(" sep…

从传统到智能化:汽车内部通信的安全挑战与SecOC解决方案

01/需求背景 Demand background 在传统的汽车电子结构中&#xff0c;车内的电控单元&#xff08;ECU&#xff09;数量和复杂性受到限制&#xff0c;通信带宽也受到限制。因此&#xff0c;人们普遍认为车内各个ECU之间的通信是可靠的。只要ECU节点接收到相应的消息&#xff0c…