Thymeleaf 的 th:text 覆盖问题

本文介绍了在SpringBoot结合Thymeleaf时如何正确处理变量不存在的情况,通过使用三元运算符实现默认值的设置,确保页面能正常显示预期内容。

在使用SpringBoot加Thymeleaf时,写了如下代码:

<p th:text="${username}">用户名</p>

这段代码的想要的效果是如果上下文存在username,那么就显示usernam,如果没有username,则显示用户民,但是经过测试结果发现,如果username在上下文中存在,显示username的值,但问题时,若不存在,则为空白,不显示”用户名“,这不是想要的效果,然后将代码进行修改。

<p th:text="${username?:'用户名'}"></p>

再次运行测试,成功。

注:因为原来看到下面这些,所以才会出现第一段代码的问题,是这段代码有误,还是自己的理解有误,希望有大佬看到能说一下,先谢过了!!!!!!!
在这里插入图片描述

表格内容太多怎么隐藏悬停才展示完整内容<div style="max-height: 900px;overflow: auto;"> <table class="table table-hover table-bordered" style="margin-bottom: 0;"> <thead class="bg-light" style="position: sticky; top: 0; z-index: 1;"> <tr class="head-table"> <th>序号</th> <th>异常标识码</th> <th>操作</th> <th>异常类型</th> <th>工段</th> <th>异常来源</th> <th>异常项目</th> <th>异常等级</th> <th>设备</th> <th>批次号</th> <th>站点</th> <th>工艺流程</th> <th>产品料号</th> <th>异常描述</th> <th>异常CODE类型</th> <th>异常数据</th> <th>异常开始时间</th> <th>是否处理</th> <th>处理人员</th> <th>处理备注</th> <th>结案时间</th> </tr> </thead> <tbody> <tr th:each="data,iterStat : ${page.records}" class="center-table"> <td th:text="${iterStat.count}"></td> <td th:text="${data.ALARMTIMEKEY}"></td> <td th:text="${iterStat.count}"></td> <td th:text="${data.ISSUETYPE}"></td> <td th:text="${data.FACTORYNAME}"></td> <td th:text="${data.ALARMTYPE}"></td> <td th:text="${data.ALARMID}"></td> <td th:text="${data.ALARMLEVEL}"></td> <td th:text="${data.MACHINENAME}"></td> <td th:text="${data.LOTNAME}"></td> <td th:text="${data.PROCESSOPERATIONNAME}"></td> <td th:text="${data.PROCESSFLOWNAME}"></td> <td th:text="${data.PRODUCTSPECNAME}"></td> <td th:text="${data.ALARMCOMMENT}"></td> <td th:text="${data.REASONCODETYPE}"></td> <td th:text="${data.REASONCODE}"></td> <td th:text="${data.START_TIME}"></td> <td th:text="${data.SOLVEFLAG}"></td> <td th:text="${data.SOLVE_USER}"></td> <td th:text="${data.SOLVE_REASON}"></td> <td th:text="${data.END_TIME}"></td> </tr> <!-- 空数据提示 --> <tr th:if="${page.records.isEmpty()}"> <td colspan="21" class="text-center text-muted">暂无数据</td> </tr> </tbody> </table> </div>
03-08
`<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Mask Status Report</title> <style> table { border-collapse: collapse; width: 100%; font-family: Arial, sans-serif; } th, td { border: 1px solid #ddd; padding: 8px; text-align: left; position: relative; } .td-content{ height: 100%; position: absolute; top: 0; padding: 0; width: 100%; left:0; display: flex; align-items: center; justify-content: center; } th { background-color: #f2f2f2; font-weight: bold; } .status-ok { background-color: #d4edda; } .status-onHold { background-color: #fff3cd; } .choice-yes { background-color: #66FF9957; } .choice-no { background-color: #FFCC9957; } .font-center{ text-align: center; } </style> </head> <body> <h2>Mask Status Report</h2> <h2 th:text="${productBody} + '-JDV review会签完成,rls mask/total mask:' + ${rlsMask}"></h2> <table> <thead> <tr> <!-- 固定表头 --> <th>Mask ID</th> <th>Mask Code</th> <th>Layer Name</th> <th>JDV Status</th> <!-- 固定属性列 --> <th th:each="frameRecord : ${fixedFrameProps}"> <span th:text="${frameRecord.getValue()}"></span> </th> <!-- 动态用户列 --> <th th:each="userId : ${dynamicUserNames}"> <span th:text="${userId}"></span> </th> </tr> </thead> <tbody> <tr th:each="data : ${maskDataList}"> <!-- 固定数据 --> <td th:text="${data.maskId}"></td> <td th:text="${data.maskCode}"></td> <td th:text="${data.layerName}"></td> <td class="font-center" th:classappend="'status-' + ${data.getJdvStatus()}" th:text="${data.getJdvStatus()}"></td> <!-- 固定属性数据 --> <td th:each="prop : ${fixedFrameProps}"> <div class="td-content" th:each="frameRecord : ${data.jdvSignatureFormFrameRecordList}" th:if="${frameRecord.prop} == ${prop.key}" th:classappend="'choice-' + ${frameRecord.choice}"> <span th:text="${frameRecord.choice}"> </span> </div> </td> <!-- 动态用户数据 --> <td th:each="userId : ${dynamicUserNames}"> <div class="td-content" th:each="chipRecord : ${data.jdvSignatureFormChipRecordList}" th:if="${chipRecord.userId} == ${userId}" th:classappend="'choice-' + ${chipRecord.choice}"> <span th:text="${chipRecord.choice}"> </span> </div> </td> </tr> </tbody> </table> </body> </html> 现在有一个Map<String, UserEntity> userIdMap集合,如果key和dynamicUserNames的userId相同则,赋值UserEntity中的userWorkId/name
11-01
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值