关于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用标签给页面select下拉框赋值的实现方法:

1、定义type.java

public class Type {

   private Integer id

   private String type

  ...getters and setters

}

2、定义SeedStarterMngController.java,将来给select的option填充值:

@ModelAttribute("allTypes")

   public List<Type>populateTypes() {

       Type type1 = new Type()

       type1.setId(1)

       type1.setType("OUTDOOR")

       Type type2 = new Type()

       type2.setId(2)

       type2.setType("INDOOR")

       List<Type>tipos = new ArrayList<Type>()

       tipos.add(type1)

       tipos.add(type2)

       return tipos

   }

3、填充方法实现页面seedstartermng.html:

<select th:field="*{type}">

       <option th:each="type : ${allTypes}" th:value="${type.id}" th:text="${type.type}">typeSelect</option>

</select>

4、实现效果:


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

原文地址: http://www.outofmemory.cn/bake/11822328.html

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

发表评论

登录后才能评论

评论列表(0条)

保存