以下是《thymeleaf3.0.5中文参考手册.pdf》中的代码,官方代码没有具体去查询。

<div th:switch="${user.role}">
<p th:case="'admin'">User is an administrator</p>
<p th:case="#{roles.manager}">User is a manager</p>
</div>

这样最终生成代码总会多出一个div。例如

<div>
<p>User is an administrator</p>
</div>

以下是笔者的代码。

user实体绑定实体ApiUserDto相关字段如下

@Data
public class ApiUserDto extends ApiUser {
private String certStatusLabel;
} @Data
public class ApiUser{
/***
* 审核状态 0 un certified //未认证(默认)
* 1 unaudited //待审核
* 2 certified //已认证
*/
private Integer certStatus;
}
<div th:switch="${user.certStatus}">
<div th:case="0" th:text="未认证" class="I124_name pageChange" data-page="/approve/index"></div>
<div th:case="1" th:text="待审核" class="I124_name pageChange" data-th-data-page="${user.type=='personal'?'/approve/personal':'/approve/company'}"></div>
<div th:case="2" th:text="已认证" class="I124_name pageChange" data-th-data-page="${user.type=='personal'?'/approve/personal':'/approve/company'}"></div>
</div>

生成效果

 显然在以下情况下,那个class并没有 最外层div,样式效果有可能就有变化了。走样?

解决方法1:

在sql语句层面额外生成一个属性certStatusLabel绑定到ApiUserDto,其他属性在页面上使用SpringEL表达式判断

select
case cert_status
WHEN 0 THEN '未认证'
WHEN 1 THEN '待审核'
WHEN 2 THEN '已认证'

END certStatusLabel,

count(n.id) notificationCount,
count(u.id) appCount,
IFNULL(u.avatar_url,'head_portrait2.png') avatar_url,
u.*
from api_user u left JOIN api_notification n
on n.user_id = u.id
LEFT JOIN api_app a on a.user_id = u.id
where u.login_name = #{loginName} and u.`password` = #{encryptedPassword}

笔者前端代码变成以下类似形式:

<div th:text="${user.certStatusLabel}" class="I124_name pageChange" data-th-data-page="${user.certStatus==0?'/approve/index':(user.certStatus==1)}"></div>

最终生成代码:

弊端:此种情况维护的时候需要修改sql语句重启web服务器

完美解决方法:

使用th:block

<th:block th:switch="${user.certStatus}">
<div th:case="0" th:text="未认证" class="I124_name pageChange" data-page="/approve/index"></div>
<div th:case="1" th:text="待审核" class="I124_name pageChange" data-th-data-page="${user.type=='personal'?'/approve/personal':'/approve/company'}"></div>
<div th:case="2" th:text="已认证" class="I124_name pageChange" data-th-data-page="${user.type=='personal'?'/approve/personal':'/approve/company'}"></div>
</th:block>

最终生成代码:

 弊端:生成代码上下文有空行,当然可以那几个case放到同一样,不过不方便维护,对于强迫症患者,来说比较致命。

如果你知道怎么把空行去除了,欢迎留言,笔者也是一个强迫症患者:)。

参考来源:

https://stackoverflow.com/questions/29657648/thymleaf-switch-statement-with-multiple-case

thymeleaf中switch case多出一个空行的更多相关文章

  1. Javascript 中 switch case 等于 (== )还是 恒等于(===)?

    Javascript 中 switch case 等于 (== )还是 恒等于(===)? 可以测试一下以下代码,这个 case 中是 等于(==)还是恒等于(===) <script> ...

  2. asp.net ashx处理程序中switch case的替代方案总结

    目录 1.用委托字典代替switch...case; 2.利用反射替代switch...case: 3.比较两种方案 4.其他方案 4.说明 5.参考 在开发 asp.net 项目中,通常使用一般处理 ...

  3. python中Switch/Case实现

    学习Python过程中,发现没有switch-case,过去写C习惯用Switch/Case语句,官方文档说通过if-elif实现.所以不妨自己来实现Switch/Case功能. 方法一 通过字典实现 ...

  4. Python里如何实现C中switch...case的功能

    python没有switch case 不过可以通过建立字典实现类似的功能 例子:根据输入的年月日,判断是该年中的第几天 y = int(input('请输入年:')) m = int(input(' ...

  5. C# 中Switch case 返回不止用break

    Switch(temp) { case "A": //跳出循环 break; case "B": //返回值 return var; case "C& ...

  6. js中switch/case分支的值可以是变量或表达式

    在一些高级语言如C#中,switch分支的值只能是常量,而js中可以是变量或表达式: <!DOCTYPE html> <html lang="en"> &l ...

  7. sql中关于case when的一个例子

    SELECT rownum R, a.expert_id as USERID, a.expert_id as TYPE, b.type_desc as TYPE_DESC, a.sex as SEX, ...

  8. Android中在activity中弹出一个popwindow

    //-----在onCreate方法--中------创建popwindow布局   --pop_item--------------------------        View view=Lay ...

  9. Java switch case和数组

    Java switch case 语句 switch case 语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支. 语法 switch case 语句格式: switch(express ...

随机推荐

  1. java中创建对象的方式

    Java中有5种创建对象的方式,下面给出它们的例子还有它们的字节码 使用new关键字 } → 调用了构造函数 使用Class类的newInstance方法 } → 调用了构造函数 使用Construc ...

  2. msaa mrt load store action unity

    unity buildin renderpipeline 和lightweight rp 对于开了msaa的rt 的load store action设置失效 buildin的时候set render ...

  3. pandas 3

    参考资料:https://mp.weixin.qq.com/s/9z3JVBkZpasC_F0ar_7JJA 删除多列:df.drop(col_names_list, axis=1, inplace= ...

  4. Codeforces Round #438 by Sberbank and Barcelona Bootcamp (Div. 1 + Div. 2 combined) A,B,C【真的菜·】

    8说了 #include<bits/stdc++.h> using namespace std; #define int long long signed main(){ string s ...

  5. P3119 [USACO15JAN]草鉴定

    约翰有n块草场,编号1到n,这些草场由若干条单行道相连.奶牛贝西是美味牧草的鉴赏家,她想到达尽可能多的草场去品尝牧草. 贝西总是从1号草场出发,最后回到1号草场.她想经过尽可能多的草场,贝西在通一个草 ...

  6. codeforces164A

    Variable, or There and Back Again CodeForces - 164A Life is not easy for the perfectly common variab ...

  7. pmm的安装,这里推荐下载官方提供的脚本,直接执行这个脚本就可以完成安装

    脚本内容如下: #!/bin/sh set -o errexit set -o xtrace root_is_needed='no' check_command() { command -v &quo ...

  8. cas系列-自定义异常提示(五)

    cas对于异常提示可以支持多语言,默认是英语,具体文件格式大同小异.这里以cas5.x版本为例. 官方推荐使用overlay方式部署,这样你只需要关注你自定义的配置即可. 获取自定义配置文件方式: 执 ...

  9. ZR#998

    ZR#998 解法: 先把所有物品按照拿走的时间从小到大排序,拿走的时间相同就按照放上去的时间从大到小.那么一件物品上方的物品就一定会在它的前面. 考虑 $ dp $ ,设 $ f[i][j] $ 表 ...

  10. Alphat【翻译】

    翻译自:CFD-online 帖子地址:http://www.cfd-online.com/Forums/openfoam-solving/144625-alphat.html Wien3: 早上好 ...