SpringBoot中如何使用Thymeleaf操作(springboot,thymeleaf,web开发)

时间:2024-05-02 21:29:01 作者 : 石家庄SEO 分类 : web开发
  • TAG :

前言

Thymeleaf是一个适用于Web和独立环境的现代服务器端Java模板引擎。

Thymeleaf的主要目标是为您的开发工作流程带来优雅的自然模板 - 可以在浏览器中正确显示的HTML,也可以用作静态原型,从而在开发团队中实现更强大的协作。

通过Spring Framework模块,与您喜欢的工具的大量集成,以及插入您自己的功能的能力,Thymeleaf是现代HTML5 JVM Web开发的理想选择

Thymeleaf与Velocity或FreeMarker等其他模板引擎相比如何?

Velocity 和 FreeMarker 都是非常棒的软件,但是它们处理模板问题的原理与Thymeleaf 完全不同。

Thymeleaf 非常强调自然模板化——允许模板成为工作原型,而其他两个模板不允许这样做——它的语法试图(可以说)更干净,更符合当前 web开发的趋势。另外,从架构的角度来看,Velocity 和 FreeMarker 都是顺序文本处理器,而 Thymeleaf 是基于标记解析技术的。这允许 Thymeleaf 利用特定于基于标记的环境的有趣特性,特别是web。

SpringBoot使用Thymeleaf 操作步骤

第一步是引入Thymeleaf starter依赖,具体代码如下:

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

第二步是在 application.properties 添加 Thymeleaf 相关配置,具体配置如下:

server.port=8090server.servlet.context-path=/sbe#关闭Thymeleaf的缓存开发过程中无需重启spring.thymeleaf.cache=false#设置thymeleaf页面的编码spring.thymeleaf.encoding=UTF-8spring.thymeleaf.mode=HTML5#设置thymeleaf页面的后缀spring.thymeleaf.suffix=.html#设置thymeleaf页面的存储路径spring.thymeleaf.prefix=classpath:/templates/

第三步是编写访问 Thymeleaf 页面 Controller。

@Controller@RequestMapping("/hello")publicclassThymeleafHelloWrodController{@RequestMapping("/thymeleaf")publicStringhelloThymeleaf(Modelmodel){model.addAttribute("hello","helloThymeleaf!");return"hello/index";}}

Thymeleaf 页面代码如下:

<!DOCTYPEhtml><htmlxmlns:th="http://www.thymeleaf.org"><head><metacharset="UTF-8"><title>Inserttitlehere</title></head><body><pth:text="${hello}">hello</p></body></html>

测试

在游览器输入访问 Thymeleaf 页面的 Controller 的 URL:http://localhost:8090/sbe/hello/thymeleaf 进行测试,测试结果如下:
SpringBoot中如何使用Thymeleaf操作

 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:SpringBoot中如何使用Thymeleaf操作的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:易语言编程命令调用操作有哪些下一篇:

24 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18