前言

  • spring boot 2.1.1.RELEASE
  • thymeleaf 3.0

变量为 null 时,显示默认值

name?:'Unknown'
name 变量为 null 时,显示值 Unknown。等价于 name?name:'Unknown'

对象为 null 时,避免调用方法或属性出错

placeOfBirth?.city
placeOfBirth 为 null 时,不再继续调用属性 city

code?.toUpperCase()
code 为 null 时,不再继续调用方法 toUpperCase

Map 获取的元素为 null

当 map 中没有名为 name 的元素时,这样写会报错 map.name
安全的写法是这样:map['name']

如果 map 中的元素为对象时,可以这样写:map['user']?.name

List 类型数组越界

数组越界时,错误是这样的:

Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "slist[2].score" (template: "exam/papers/edit" - line 117, col 92)
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1025E: The collection has '1' elements, index '2' is invalid

SPEL 是这样的 slist[2].score,但 slist 不够3个元素(EL1025E: The collection has '1' elements, index '2' is invalid),因此数组越界了。

解决办法:添加数组大小的判断。上面的情况下,用 #lists.size(slist)>=3?slist[2]?.score:0 替换 slist[2].score

参考

https://blog.csdn.net/sayyy/article/details/108011723
https://blog.csdn.net/sayyy/article/details/108011653

【spring boot】【thymeleaf】SPEL处理 null 值相关推荐

  1. Spring Boot - Thymeleaf模板简介以及集成

    文章目录 Spring Boot - Thymeleaf模板简介以及集成 1.什么是Thymeleaf? 2.标准表达式 2.1 变量表达式 2.2 选择表达式/星号表达式 2.3 URL表达式 2. ...

  2. Spring Boot 整合 shiro 之盐值加密认证详解(六)

    Spring Boot 整合 shiro 之盐值加密认证详解 概述 不加盐认证 加入密码认证核心代码 修改 CustomRealm 新增获取密文的方法 修改 doGetAuthenticationIn ...

  3. Spring Boot + Thymeleaf 创建web项目

    本篇文章将引导你创建一个简单的Spring Boot web程序示例,涉及到的组件有:嵌入的Tomcat + Thymeleaf 模板引擎,可执行的 JAR 文件包. 开发工具: 1.Spring B ...

  4. Spring Boot笔记-自定义配置项默认值设置

    如果用动态配置中心,如果漏了配置会很麻烦,spring boot可以设置个默认值如下: 这个是正常的: application.properties test.hello=Hello World My ...

  5. Spring boot+Thymeleaf+easyui集成:js创建组件页面报错

    开发工具:Ideal 使用场景:Demo 前提:        环境:Spring boot +Thymeleaf+easyui 引入thymeleaf模板引擎 1 <html lang=&qu ...

  6. Spring boot +java.awt.HeadlessException: null异常处理

    Spring boot +java.awt.HeadlessException: null异常处理 参考文章: (1)Spring boot +java.awt.HeadlessException: ...

  7. thymeleaf的能用在什么地方_细品 Spring Boot+Thymeleaf,还有这么多好玩的细节!

    松哥原创的 Spring Boot 视频教程已经杀青,感兴趣的小伙伴戳这里-->松哥要升级 SpringBoot 视频了,看看新增了哪些内容! 虽然现在流行前后端分离,但是后端模版在一些关键地方 ...

  8. Spring boot - Thymeleaf 使用

    1,thymeleaf 简介 thymeleaf 是一个spring boot中推荐使用的比较新的java模板引擎.其使用场景可以在html.javascript.css.xml.text当中.在官方 ...

  9. 细品 Spring Boot+Thymeleaf,还有这么多好玩的细节!

    文章目录 1. Thymeleaf 简介 2. 整合 Spring Boot 2.1 基本用法 2.2 手动渲染 3. Thymeleaf 细节 3.1 标准表达式语法 3.1.1 简单表达式 3.1 ...

最新文章

  1. wPaint在线绘图插件
  2. 推荐一款高端大气上档次的在线作图工具
  3. python二维数据读取对齐_从投影的二维直方图绘制对齐的x,y一维直方图
  4. [Python图像处理] 十三.基于灰度三维图的图像顶帽运算和黑帽运算
  5. Java中创建对象的5种方式
  6. rest-framework之响应器(渲染器)
  7. 33 windows_33_Proc_windows_job 进程,windows作业
  8. 下月上市!中兴AXON 10 Pro 5G版通过3C认证 加入5G大战
  9. mysql 修改这段长度_MySQL中使用group_concat()函数数据被截取(有默认长度限制),谨慎!...
  10. STP重新收敛过程和补充内容
  11. Unicode字符集和编码方式
  12. C++ Primer 5th 第15章 面向对象程序设计
  13. eclipse不自动弹出提示的解决办法(eclipse alt+/快捷键失效)centos 6.7
  14. 一款PC端抖音去水印工具V1.86
  15. 计算机显卡型号中数字含义详解,显卡型号中字母和数字都是什么意思?
  16. 谈谈EOS的出块时间,不可逆时间,BFT
  17. 怎么架设魔兽世界服务器?
  18. react生命周期方法介绍
  19. 安装配置Apache服务器
  20. java版阿里云发送短信

热门文章

  1. 使用Word宏替换Header、Footer等中的文本
  2. 你的行为合理吗?看看社会心理学给我们的启示。
  3. NCNE二级复习资料-网络监视、管理和排错
  4. Python中的ThreadLocal变量
  5. maven打包出错: Failed to clean project: Failed to delete
  6. 引用js或css后加?v= 版本号的用法
  7. asp.net Session在web.config中的三种配置方式——详解
  8. 环境搭建之日志搜集/采集服务系统对比(logtail/hadoop/elk/splunk/appender)
  9. 解密春晚微信红包十亿级并发压力
  10. 小米网抢购系统开发实践和我的个人观察