当前位置:网站首页>[thymeleaf] handle null values and use safe operators

[thymeleaf] handle null values and use safe operators

2022-04-23 14:55:00 sayyy

	<input id="treeId" name="treeId" type="hidden" th:value="${dept?.deptId?:''}"/>
	<input id="treeName" name="treeName" type="hidden" th:value="${dept?.deptName?:''}"/>
  • ${dept?.deptId}: Use safe operators , Prevent throwing NullPointerException
  • ${dept?.deptId?:''}deptId by null when , Output ''

spring el The expression sets the default value

Reference resources here .

When no default value is set ,deptId by null when , Will be output null.

	<input id="treeId" name="treeId" type="hidden" th:value="${dept.deptId}"/>

After setting the default value ,deptId by null when ,, Will be output ''.

	<input id="treeId" name="treeId" type="hidden" th:value="${dept.deptId?:''}"/>

spring el Expression security operator

Reference resources here .

After using the security operator , Don't worry about NullPointerException 了 .

When there is no safe operator ,dept Variable is null when ,${dept.deptId} Will throw out NullPointerException .

	<input id="treeId" name="treeId" type="hidden" th:value="${dept.deptId}"/>

After using the safe operator ,dept Variable is null when ,${dept?.deptId} And will not throw NullPointerException .

	<input id="treeId" name="treeId" type="hidden" th:value="${dept?.deptId}"/>

版权声明
本文为[sayyy]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231454059333.html