Thymeleaf模板在用户列表不显示当前登录用户的方法(亲测有效)

直接上代码

<table class="table table-striped table-hover table-bordered">
		<thead>
			<tr>
				<th data-field="username">账号</th>
				<th data-field="cretae_time">注册时间</th>
			</tr>
		</thead>
		<tbody class="table-condensed">
			<tr th:if="${userModel.userList.size() -1} eq 0">
				<td colspan="10" style="color:red; text-align:center;"><b>暂无用户信息!</b></td>
			</tr>
			<tr th:each="user : ${userModel.userList}" th:if="${(user.username) ne (#authentication.name)}">
				<td th:text="${user.username} ?: '(空)'">账号</td>
				<td th:text="${#dates.format(user.create_time,'yyyy年MM月dd日  HH:mm:ss')} ?: '(空)'">注册时间</td>
			</tr>
		</tbody>
	</table>

注解:

关键代码:如果后台返回过来的userModel.userList的长度减1等于0就显示"暂无用户信息",因为即使只有当前登录用户时也显示"暂无用户信息"
 th:if="${userModel.userList.size() -1} eq 0}"
关键代码:${#authentication.name}   ==>获取当前登录的用户名如果table表格中的user.username等于当前登录用户则不显示
th:if="${(user.username) ne (#authentication.name)}

代码拓展与总结:

#当前用户名
${#authentication.name}

#当前用户的xxx属性
${#authentication.principal.xxx}

2019.07.01笔,后续会继续更新,多多支持

发布了27 篇原创文章 · 获赞 3 · 访问量 2629

猜你喜欢

转载自blog.csdn.net/qq_42426937/article/details/94401652