【发布时间】:2026-01-08 21:40:01
【问题描述】:
我正在编写一个培训应用程序来收集以下堆栈上的书籍:Java/Spring/Thymeleaf。
我应该输入一些关于书籍的值,例如书名和书籍大小(页数)。
dto 类 Book 中的验证字段:
@NotEmpty
private String title;
@NotNull
@Digits(integer = 4, fraction = 0)
private Integer size;
视图文件包含:
<td>
<input type="text" placeholder="book_title" th:field="*{title}">
<p th:if="${#fields.hasErrors()} and *{title}==''">field title must not be empty</p>
</td>
<td>
<input type="text" placeholder="size (page)" th:field="*{size}">
<p th:if="${#fields.hasErrors()} and !${#size.matches('[0-9]{1,4}')}">field value must be digit and less than 4 signs</p>
它工作正常:如果title 字段未填写,我们可以看到消息。
但是,对于 size 字段,我们期望 4 位数字,但是如何正确配置此正则表达式? !${#size.matches('[0-9]{1,4}')} 这样就没有警告信息了。
我提出了类似的问题 Check if a string contains number or is numeric - Thymeleaf 但我需要反转布尔条件,它不适用于我的情况
【问题讨论】:
-
我根据这个链接编写我的正则表达式并反转布尔表达式,但它不起作用