Thymeleaf:th:text仅在不为null的情况下? [英] Thymeleaf: th:text only if not null?

查看:1595
本文介绍了Thymeleaf:th:text仅在不为null的情况下?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有类似下面的内容,如果用户先前输入了错误的凭据,那当然可以工作.

I have something like below, which of course works if user had previously input his wrong credentials.

但是,例如,如果我直接进入登录失败网址/login?error,例如以前没有任何不正确的登录,则session[SPRING_SECURITY_LAST_EXCEPTION]当然是null,并且我会收到讨厌的 404 .

However if I directly go to my login fail url /login?error for example without any previous incorrect logins, session[SPRING_SECURITY_LAST_EXCEPTION] is of course null and I get a nasty 404.

<span th:text="${session[SPRING_SECURITY_LAST_EXCEPTION].message}">Invalid credentials</span>

问题:

下面是否有用于以下内容的处理器(大部分时间读写时间太长),还是应该自己滚动?

Is there a processor for something like below (which is too long to write and read most of the time), or should I just roll my own?

<span th:text="${session[SPRING_SECURITY_LAST_EXCEPTION] != null ? session[SPRING_SECURITY_LAST_EXCEPTION].message : #messages.msg('AbstractUserDetailsAuthenticationProvider.badCredentials')}">
    Invalid credentials
</span>

推荐答案

您可以使用th:if语句:

You can use the th:if statement:

<span th:if="${session[SPRING_SECURITY_LAST_EXCEPTION].message}" th:text="${session[SPRING_SECURITY_LAST_EXCEPTION].message}">Invalid credentials</span>

只有在存在值的情况下才会显示此行.

That will only display this line if there is a value present.

额外检查:

<span th:if="${session[SPRING_SECURITY_LAST_EXCEPTION] != null and session[SPRING_SECURITY_LAST_EXCEPTION].message != null}" th:text="${session[SPRING_SECURITY_LAST_EXCEPTION].message}">Invalid credentials</span>

这篇关于Thymeleaf:th:text仅在不为null的情况下?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆