thymeleaf

SpringBoot之Thymeleaf模板引擎如何运用?案例详解

♀尐吖头ヾ 提交于 2020-08-05 04:20:55
根据B站狂神学java的笔记 模板引擎Thymeleaf 前端想要显示数据,我们以前是把页面转换成jsp。这样我们就能够实现数据的显示,及交互等。 jsp支持非常强大的功能,包括能写Java代码 。但是我们使用SpringBoot项目是jar方式而不是war。我们还是使用嵌入式的Tomcat,但现在springBoot默认是不支持jsp的。那该使用什么呢? 第二要注意 : 光理论是不够的。在此免费赠送5大JAVA架构项目实战教程及大厂面试题库,有兴趣的可以进裙 783802103获取,没基础勿进哦! SpringBoot推荐你可以来使用模板引擎: 模板引擎,我们其实大家听到很多,其实jsp就是一个模板引擎,还有用的比较多的freemarker,包括SpringBoot给我们推荐的Thymeleaf,模板引擎有非常多,但再多的模板引擎,他们的思想都是一样的,什么样一个思想呢我们来看一下这张图: 模板引擎的作用就是使用表达式解析后台的数据。 引入Thymeleaf 怎么引入呢,对于springboot来说,什么事情不都是一个start的事情嘛,我们去在项目中引入一下。给大家三个网址: Thymeleaf 官网: https://www.thymeleaf.org/ Thymeleaf 在Github 的主页: https://github.com/thymeleaf/thymeleaf

SpringBoot切面Aop的demo简单讲解

删除回忆录丶 提交于 2020-08-04 20:00:07
前言 本篇文章主要介绍的是SpringBoot切面Aop的demo简单讲解。 SpringBoot Aop 说明:如果想直接获取工程那么可以直接跳到底部,通过链接下载工程代码。 切面(Aop) 一、概念 AOP(Aspect OrientedProgramming):面向切面编程,面向切面编程(也叫面向方面编程),是目前软件开发中的一个热点,也是Spring框架中的一个重要内容。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。 二、用途 日志记录,性能统计,安全控制,权限管理,事务处理,异常处理,资源池管理。 三、详解 1.切面(Aspect): 官方的抽象定义为“一个关注点的模块化,这个关注点可能会横切多个对象”,在本例中,“切面”就是类TestAspect所关注的具体行为,例如:AServiceImpl.barA()的调用就是切面TestAspect所关注的行为之一。“切面”在ApplicationContext中 aop:aspect 来配置。 2.连接点(Joinpoint): 程序执行过程中的某一行为,例如,AServiceImpl.barA()的调用或者BServiceImpl.barB(String _msg, int _type)抛出异常等行为。 3.通知(Advice): “切面

thymeleaf中的fragment使用

浪子不回头ぞ 提交于 2020-08-04 16:16:36
fragment介绍 fragment类似于JSP的tag,在html中文件中,可以将多个地方出现的元素块用fragment包起来使用。 fragment使用 定义fragment 所有的fragment可以写在一个文件里面,也可以单独存在,例如 <footer th:fragment="copy"> the content of footer </footer> fragment的引用 th:insert:保留自己的主标签,保留th:fragment的主标签。 th:replace:不要自己的主标签,保留th:fragment的主标签。 th:include:保留自己的主标签,不要th:fragment的主标签。(据说,官方3.0后不推荐) 导入片段: <div th:insert="footer :: copy"></div> <div th:replace="footer :: copy"></div> <div th:include="footer :: copy"></div> 结果为: <div> <footer> the content of footer </footer> </div> <footer> the content of footer </footer> <div> the content of footer </div> 在Springboot中

Thymeleaf货币转换

眉间皱痕 提交于 2020-07-29 07:32:11
#概述 本文,将介绍如何使用页面组件Thymeleaf对货币进行自动转换 #Maven依赖 < dependency > < groupId > org.springframework.boot </ groupId > < artifactId > spring-boot-starter-thymeleaf </ artifactId > < version > 2.3.0.RELEASE </ version > </ dependency > #创建thymeleaf页面和Controller 在resources/templates/下创建页面currencies.html <!DOCTYPE html> < html xmlns = "http://www.w3.org/1999/xhtml" xmlns:th = "http://www.thymeleaf.org" > < head > < meta charset = "UTF-8" > < title > Currency Format </ title > </ head > < body > < h3 th:text = "${#numbers.formatCurrency(param.amount)}" > </ h3 > </ body > </ html > 创建CurrencyController

Thymeleaf读取国际化文本时出现??xxxxxx_zh_CN??问题

ぐ巨炮叔叔 提交于 2020-07-29 06:04:38
最近在学习thymeleaf模板引擎,在使用th:text读取国际化文本时读取不到值,如下: 资源目录结构如下: index.html 内容: <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <p th:text="#{home.welcome}"></p> </body> </html> home.properties 内容: home.welcome=你好,thymeleaf! 在度娘的帮助下,找到了问题的所在, spring.messages.basename 默认值为 messages ,根据properties的位置修改为: spring: messages: basename: msg/home 再次访问,值倒是能读取出来了,只是中文变成乱码了,结果如下,出现了编码问题: 在确认index.html的编码和 spring.messages.encoding 的值都为utf-8后,突然想到会不会是home.properties本身编码不对呢,马上打开idea的编码设置界面,果然,properties文件默认的编码为 GBK : 修改默认编码为UTF-8后,再修改home

Java Spring Boot VS .NetCore (五)MyBatis vs EFCore

耗尽温柔 提交于 2020-07-28 07:34:11
介绍 添加MyBatis先关的依赖包 <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency> MyBatis 有两种配置方式 一种是通过XML来实现 相关操作 解耦性相对较强,第二种则是以注解的方式来实现,这里已第二种方式说明 使用这种方式还需要配置下 指向实体类的包路径 mybatis.type-aliases- package =com.liyouming.demo.domain.Entitys 在 启动入口里面添加注解指向 Mapper操作功能包地址 @MapperScan("com.liyouming.demo.Mapper") 下面的代码是功能类User 直接可以通过注解的方式完成相关方法的操作 public interface UserMapper { @Select( "SELECT

【springmvc thymeleaf】springmvc整合thymeleaf

☆樱花仙子☆ 提交于 2020-07-28 07:00:43
概述 Thymeleaf提供了一组Spring集成,使您可以将其用作Spring MVC应用程序中JSP的全功能替代品。 这些集成将使您能够: @Controller像使用JSP一样,将Spring MVC 对象中的映射方法转发到Thymeleaf管理的模板。 在模板中使用Spring表达式语言(Spring EL)代替OGNL。 在与表单支持Bean和结果绑定完全集成的模板中创建表单,包括使用属性编辑器,转换服务和验证错误处理。 显示Spring管理的消息文件中的国际化消息(通过常规MessageSource对象)。 使用Spring自己的资源解析机制解析您的模板。 thymeleaf自己也做了spring的集成,所以我们并不需要做太多的配置,就可以达到我们想要的结果。thymeleaf提供了两种集成方法:①、注解配置,也就是java代码,②、xml文件配配置,本文主要介绍第二种xml配置。 你能get到的知识点: 1、springmvc整合thymeleaf 2、spring提供的三种model的使用 3、解决html前端thymeleaf不生效问题(见问题1) 4、解决html前端显示乱码问题(见问题2) @ 目录 概述 你能get到的知识点: springmvc整合thymeleaf 一:加入依赖 二:设置thymeleaf解析器 三:编写控制器 1、使用Model 2

SpringBoot+Thymeleaf实现数据渲染

孤街浪徒 提交于 2020-07-27 12:26:11
目录 Thymeleaf简介 1.是什么 2.优点 3.常用标签 4.标准表达式语法 SpringBoot+Thymeleaf交互 1.交互代码 2.关键代码解析 Thymeleaf简介 1.是什么 Thymeleaf是spring boot推荐使用的模板语法,它可以完全替代 JSP 。 从代码层次上讲:Thymeleaf是一个java类库,它是一个xml/xhtml/html5的模板引擎,可以作为mvc的web应用的view层。 2.优点 开箱即用,它提供标准和spring标准两种方言,可以直接套用模板实现JSTL、 OGNL表达式效果,避免每天套模板、改jstl、改标签的困扰。同时开发人员也可以扩展和创建自定义的方言; Thymeleaf 提供spring标准方言和一个与 SpringMVC 完美集成的可选模块,可以快速的实现表单绑定、属性编辑器、国际化等功能。 有网无网的情况下模版页面都可以执行,美工的页面拿来就可以用,相对jsp减少了额外的标签,页面也更加简洁。 3.常用标签 属性 作用 优先级(数字越小,优先级越高) th:text 设置当前元素的文本内容 7 th:value 设置当前元素的value值,类似修改指定html标签属性的还有th:src,th:href 6 th:each 遍历循环元素,和th:text或th:value一起使用 2 th:if 条件判断

Getting “Neither BindingResult nor plain target object for bean name 'bean name' available as request attribute” when looping

柔情痞子 提交于 2020-07-23 08:02:30
问题 I am trying to get Multipile Forms to put information into. Those forms are all identical, except in the type information. That is represented in Java in the Type enun interface. All fields of the form should go into a Color object preferrably. If I do this without the loop, and change, what is given to thymeleaf, it works. I am running this with Java 1.8.0_231, spring-core 5.1.6, springboot 2.1.4, thymeleaf 3.0.11 Note: this is not the full list! Below is my current relavent code: My problem