使用thymelaf显示错误消息会更改登录表单的布局



我使用Thymelaf在输入错误凭据时在登录表单上显示错误消息:

<body>
<div class="container">
<form class="form" id="login" th:action="@{/login}" method="post">
<h1 class="form__title">Login</h1>
<div th:if="${param.error}" class="form__message form__message-error">
invalid username or password
</div>
</div>
</body>

由于这个错误消息div只在输入错误凭据时出现,所以我的表单会更改其布局——输入字段向下移动。我不想要这个。无论哪种方式,我都希望表单看起来相同,只是应该写入或删除消息。有人知道如何做到这一点吗?

th:if只会在存在error请求参数时添加<div>。要做您想做的事情,您应该使用CSS来有一个空的div或显示一些文本,使用th:classappend来选择要使用的CSS类。

类似于:

<body>
<div class="container">
<form class="form" id="login" th:action="@{/login}" method="post">
<h1 class="form__title">Login</h1>
<div th:classappend="${param.error}?'show':'hide'" class="form__message form__message-error">
invalid username or password
</div>
</div>
</body>

使用CSS中定义的.show.hide类。

最新更新