需求
注:examid
是tpGlobalExammessage
对象的属性
<form id="form-notice-edit" th:object="${tpGlobalExammessage}">
<input th:field="*{examid}" type="hidden">
</form>
以上代码逻辑上面没有问题,但是当后台取到的tpGlobalExammessage
对象为空时,由于examid
同样为空则,程序会报错
EL1007E: Property or field 'examid' cannot be found on null
看了许多博客,发现了一个很神奇的办法,代码改为
<form class="form-horizontal m" id="form-notice-edit" th:object="${tpGlobalExammessage}">
<input id="examid" name="examid" th:value="${tpGlobalExammessage?.examid}" type="hidden">
</form>
简单点说就是讲${tpGlobalExammessage.examid}
改为${tpGlobalExammessage?.examid}
的取值方式,具体原因不清楚,加上?
之后examid
为空则不影响程序的运行,同时当examid
不为空时,此方式仍能渠道它的值
果断小本本记下,以备日后使用