为什么不显示空白注释消息?

发布于 2025-01-14 16:17:36 字数 2934 浏览 0 评论 0原文

我正在开发 spring mvc Web 应用程序。在应用程序中,我在注册表单中的 username 字段上添加了 @NotBlank 注释以进行验证。我填写了 username 字段,然后单击 Submit 按钮,当时我收到验证错误消息,但我填写了 username 字段,所以为什么 BindingResultusername 字段后面产生错误,并且如果插入少于 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:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

韶华倾负 2025-01-21 16:17:36

问题就在这里,

<input type="text" 
       class="form-control" 
       th:value="${registration.username}" 
       id="username"/>

您缺少 name 属性。

<input type="text" 
       class="form-control" 
       th:value="${registration.username}" 
       id="username" 
       name="username"/>

这会修复它。

表单通常使用 input 标记的 name 属性绑定到一个对象 - Registration(在您的情况下)。注释正在工作,问题是由于缺少属性,您在 Registration 中收到 null 用户名。

The problem is here

<input type="text" 
       class="form-control" 
       th:value="${registration.username}" 
       id="username"/>

You are missing the name attribute.

<input type="text" 
       class="form-control" 
       th:value="${registration.username}" 
       id="username" 
       name="username"/>

This will fix it.

The form normally is bound to an object - Registration in your case, using the name attributes of input tags. The annotations are working, the problem is that you are receiving null username in Registration due to that missing attribute.

枫以 2025-01-21 16:17:36

您的输入需要使用绑定语法 *{..}。替换:

<input type="text" 
            class="form-control"
            th:value="${registration.username}"
            id="username" />

为:

<input type="text" 
            class="form-control"
            th:value="*{username}"
            id="username" />

Your input needs to use the binding syntax *{..}. Replace:

<input type="text" 
            class="form-control"
            th:value="${registration.username}"
            id="username" />

with:

<input type="text" 
            class="form-control"
            th:value="*{username}"
            id="username" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文