为什么不显示空白注释消息?
我正在开发 spring mvc Web 应用程序。在应用程序中,我在注册表单中的 username
字段上添加了 @NotBlank
注释以进行验证。我填写了 username
字段,然后单击 Submit
按钮,当时我收到验证错误消息,但我填写了 username
字段,所以为什么 BindingResult
在 username
字段后面产生错误,并且如果插入少于 3 个或大于 12 个字符,我的 @Size
注释将不起作用。
下面是代码:
实体:
public class Registration {
@NotBlank(message = "Username can not be empty !")
@Size(min = 3, max = 12, message = "Username must be in 3 to 12 character") // Not working
private String username;
private String email;
private Boolean agree;
// constructor, getter, setter and toString()
}
控制器:
@GetMapping("/form")
public String formPage(Model mdl)
{
mdl.addAttribute("registration", new Registration());
return "form";
}
@PostMapping("/register")
public String registerUser(@Valid @ModelAttribute("registration") Registration registration,
BindingResult result)
{
if(result.hasErrors())
{
System.out.println(result);
return "form";
}
return "success";
}
form.html:
<form class="bg-white p-5" th:action="@{/register}" method="post" th:object="${registration}">
<h1 class="text-center pt-2 pb-2">Signup Form</h1>
<div class="mb-3">
<label for="username" class="form-label">Username</label>
<input type="text"
class="form-control"
th:value="${registration.username}"
id="username" />
<p th:each="error : ${#fields.errors('username')}" th:text="${error}">
</p>
</div>
<div class="mb-3">
<label for="email" class="form-label">Email address</label>
<input type="text"
class="form-control"
id="email"
th:value="${registration.email}"
aria-describedby="emailHelp" />
</div>
<div class="mb-3 form-check">
<input type="checkbox"
class="form-check-input"
id="exampleCheck1">
<label class="form-check-label" for="exampleCheck1">Check me out</label>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
用于验证的Xml:
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
问题快照:
I am working on spring mvc web application. In application, I put @NotBlank
annotation for validation on username
field in sign up form. I fill the username
field and click on Submit
button that time i get validation error message, but I fill username
field so why BindingResult
produce error behind username
field and my @Size
annotaion is not worked if insert less than 3 or greater than 12 character.
Here down is code:
Entity:
public class Registration {
@NotBlank(message = "Username can not be empty !")
@Size(min = 3, max = 12, message = "Username must be in 3 to 12 character") // Not working
private String username;
private String email;
private Boolean agree;
// constructor, getter, setter and toString()
}
Controller:
@GetMapping("/form")
public String formPage(Model mdl)
{
mdl.addAttribute("registration", new Registration());
return "form";
}
@PostMapping("/register")
public String registerUser(@Valid @ModelAttribute("registration") Registration registration,
BindingResult result)
{
if(result.hasErrors())
{
System.out.println(result);
return "form";
}
return "success";
}
form.html:
<form class="bg-white p-5" th:action="@{/register}" method="post" th:object="${registration}">
<h1 class="text-center pt-2 pb-2">Signup Form</h1>
<div class="mb-3">
<label for="username" class="form-label">Username</label>
<input type="text"
class="form-control"
th:value="${registration.username}"
id="username" />
<p th:each="error : ${#fields.errors('username')}" th:text="${error}">
</p>
</div>
<div class="mb-3">
<label for="email" class="form-label">Email address</label>
<input type="text"
class="form-control"
id="email"
th:value="${registration.email}"
aria-describedby="emailHelp" />
</div>
<div class="mb-3 form-check">
<input type="checkbox"
class="form-check-input"
id="exampleCheck1">
<label class="form-check-label" for="exampleCheck1">Check me out</label>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
Xml for validation:
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
Snapshot of problem:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题就在这里,
您缺少
name
属性。这会修复它。
表单通常使用
input
标记的name
属性绑定到一个对象 -Registration
(在您的情况下)。注释正在工作,问题是由于缺少属性,您在Registration
中收到null
用户名。The problem is here
You are missing the
name
attribute.This will fix it.
The form normally is bound to an object -
Registration
in your case, using thename
attributes ofinput
tags. The annotations are working, the problem is that you are receivingnull
username inRegistration
due to that missing attribute.您的输入需要使用绑定语法
*{..}
。替换:为:
Your input needs to use the binding syntax
*{..}
. Replace:with: