关于thymeleaf的使用

关于thymeleaf的使用,第1张

浏览器忽略一切不能够识别的属性

例如 th:text="xxxx" ,这样的属性浏览器会忽略,对于html5的标准属性写法 也是支持的

属性只需要 data-开头  例如 :data-th-text(html5标准写法)=>th:text写法

加入这个原因官方解释为了 不让我们的IDE在校验html文件时 出现烦人的警告。

常用属性

th:text--计算表达式的值,把结果 填充到 当前的标签内 即tags 的 body替换,类似于 jquery 的 text() 方法。

th:utext--与 th:text类似 但是其不转码表达式的计算结果  比如 这种标签类的文本计算出来后 会被 th:text转码

表达式语法

变量访问表达式 ${....},例如 ${user.name}

消息访问表达式 #{...},主要是访问 配置文件中的值 支持国际化的显示

url表达式 @{....} 处理url使用

片段表达式 ~{...} 引入其他片段使用

表达式中 可用的字面量

字符串:用单引号包裹 ‘this is demo’  例如

111

param.true22

也可以省略 单引号

数字 : 123.1  0.1 9 

-1.5

布尔:true false 

null 字面量: null

字面量标记: one  param1 等

字符串连接 用加号  ‘this is’+'demo'

文字替换 |this is demo ${test}|  其中 test为 某个变量或者 变量的字面量标记

二元运算符  + - * / %  加 减 乘 除 取余

一元运算符 - 取负

二元逻辑运算  or  and

一元逻辑运算 ! not  都是取反的意思

比较运算符  >, <, >= , <= ( gt , lt , ge , le )

等值比较  == , != ( eq , ne )

条件判断  (if)? (then)

(if) ? (then) : (else)

(value) ?: (defaultvalue)

多语言的支持

Welcome to our grocery store!

thymeleaf

th:text="#{}"---获取 properties文件中的 数据

html5的格式可以写成  data-th-text=""

${x} will return a variable x stored into the Thymeleaf context or as a request attribute.

${param.x} will return a request parameter called x (which might be multivalued).

${session.x} will return a session attribute called x .

${application.x} will return a servlet context attribute called x .

想要 th:text展示标签 即富文本展示的时候 采用  等   使用 th:utext代替

like ${user.name} for “get the variable called user, and call its getName() method”).

Variable Expressions: ${...}

Selection Variable Expressions: *{...}

Message Expressions: #{...}

Link URL Expressions: @{...}

Fragment Expressions: ~{...}

一个综合的表达式

'User is of type ' + (${user.isAdmin()} ? 'Administrator' : (${user.type} ?: 'Unknown'))

消息中嵌入参数

Welcome to our grocery store, Sebastian Pepper!

多个参数用逗号分开 

key值也可以用参数

Welcome to our grocery store, Sebastian Pepper!

#ctx : the context object.

#vars: the context variables.

#locale : the context locale.

#request : (only in Web Contexts) the HttpServletRequest object.

#response : (only in Web Contexts) the HttpServletResponse object.

#session : (only in Web Contexts) the HttpSession object.

#servletContext : (only in Web Contexts) the ServletContext object.

Established locale country: US.

使用星号表达式

Name: Sebastian.

Surname: Pepper.

Nationality: Saturn.

而且 $和 *的取值方式还能通用

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

Thymeleaf 的主要目标是为开发工作流程带来优雅的自然模板ー HTML,它既可以在浏览器中正确显示,也可以作为静态原型工作,从而加强开发团队之间的协作。

有了 Spring Framework 的模块、大量与您最喜欢的工具集成的功能,以及插入您自己功能的能力,Thymeleaf 是现代 HTML5 JVM web 开发的理想选择ーー尽管它可以做的还有很多。

用 Thymeleaf 语言编写的 HTML 模板看起来和工作方式仍然类似于 HTML,使得在应用程序中运行的实际模板仍然可以作为有用的设计工件工作。

Thymeleaf 3.0.11.RELEASE is the current stable version. It requires Java SE 6 or newer.

For the old 2.1.x branch, Thymeleaf 2.1.6.RELEASE is the latest version.

称为 Standard 和 SpringStandard ,这些方言定义了一组特性,对于大多数场景来说,这些特性应该足够了。 你可以识别这些标准方言何时在模板中使用,因为它包含以 th 前缀开头的属性,比如 span th: text..."。

Variable expressions.

Variable expressions

所以这相当于:

消息表达式(通常称为文本外部化、国际化或 i18n)允许我们从外部源(。 属性文件) ,通过键引用它们,并(可选地)应用一组参数。

因此,对于部署在 web 服务器的 / myapp 上下文中的 web 应用程序,可以使用如下表达式:

可以转换成这样的东西:

Url 也可以带参数:

导致这样的结果:

链接表达式可以是相对的,在这种情况下,没有应用程序上下文将前缀的 URL:

还有服务器相关(同样,没有应用程序上下文可以前缀) :

和 protocol-relative (就像绝对 url 一样,但是浏览器将使用与显示页面相同的 HTTP 或 HTTPS 协议) :

当然,Link 表达式可以是绝对的:

但是它们可以在任何地方使用,就像任何其他变量一样:

A good bunch of types of literals and operations are available:

Literals:

Text literals: 'one text', 'Another one!',…

Number literals: 0, 34, 3.0, 12.3,…

Boolean literals: true, false

Null literal: null

Literal tokens: one, sometext, main,…

Text operations: 文本运算

String concatenation: +

Literal substitutions: |The name is ${name}|

Arithmetic operations:

Binary operators: +, -, *, /, %

Minus sign (unary operator): -

Boolean operations: 布尔运算

Binary operators: and, or

Boolean negation (unary operator): !, not

Comparisons and equality: 比较运算

Comparators: >, <, >=, <= (gt, lt, ge, le)

Equality operators: ==, != (eq, ne)

Conditional operators: 条件运算

If-then: (if) ? (then)

If-then-else: (if) ? (then) : (else)

Default: (value) ?: (defaultvalue)

让我们来看看文学语言中的一些最基本的属性。 以 th: text 开始,它只是替换了标记的主体(请再次注意这里的原型化能力) :

接下来是 each,它会重复数组或表达式返回的元素的次数,并为迭代元素创建一个内部变量,其语法与 Java foreach 表达式相同:

最后,Thymeleaf 为特定的 XHTML 和 HTML5属性包含了许多 th 属性,这些属性只是评估它们的表达式并将这些属性的值设置为它们的结果。 他们的名字模仿了他们设置的属性值:

thymeleaf th:if表达式语法

网页应用

thymeleaf th:if表达式语法

https://blog.csdn.net/weixin_38970805/article/details/82937465


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/7213039.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-03
下一篇 2023-04-03

发表评论

登录后才能评论

评论列表(0条)

保存