表单输入约束未强制执行?

发布于 2024-11-05 09:32:04 字数 1774 浏览 0 评论 0原文

我是 Tomcat 和 Spring Web 的新手。我正在尝试按照 this 使用 Spring 的表单验证功能教程。一切似乎都运行顺利,除了一件事......我的表单不进行任何验证,当我发送表单时,无论我提供哪些数据,我总是可以到达成功页面。

我是否正确使用了约束?我想强制用户填写他们的名字,并且名字至少有两个字符长。

package net.devmanuals.form;

import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.NotEmpty;

public class RegistrationForm {
    @NotEmpty(message = "You surely have a name, don't you?")
    @Size(min = 2, message = "I'm pretty sure that your name consists of more than one letter.")
    private String firstName;

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getFirstName() {
        return this.firstName;
    }
}

表单代码:

    <form:form method="post" commandName="regform">
        <p><form:input path="firstName" /> <form:errors path="firstName" /></p>
        <p><input type="submit" /></p>
    </form:form>

控制器:

@Controller
@RequestMapping("/register")
public class RegistrationController {
    @RequestMapping(method = RequestMethod.GET)
    public String showRegForm(Map model) {
        RegistrationForm regForm = new RegistrationForm();
        model.put("regform", regForm);
        return "regform";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String validateForm(@Valid RegistrationForm regForm, BindingResult result, Map model) {
        if (result.hasErrors()) {
            return "regform";
        }

        model.put("regform", regForm);
        return "regsuccess";
    }
}

我是否错误地应用了约束?

I'm new to Tomcat and Spring Web. I'm trying to use Spring's form validation features by following this tutorial. Everything seems to run smoothly except for one thing... my form doesn't do any validation and I can always get to the success page when I send the form no matter which data I provide.

Am I using the constraints correctly? I want to enforce that the user fills in their first name and that the first name be at least two characters long.

package net.devmanuals.form;

import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.NotEmpty;

public class RegistrationForm {
    @NotEmpty(message = "You surely have a name, don't you?")
    @Size(min = 2, message = "I'm pretty sure that your name consists of more than one letter.")
    private String firstName;

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getFirstName() {
        return this.firstName;
    }
}

Form code:

    <form:form method="post" commandName="regform">
        <p><form:input path="firstName" /> <form:errors path="firstName" /></p>
        <p><input type="submit" /></p>
    </form:form>

The controller:

@Controller
@RequestMapping("/register")
public class RegistrationController {
    @RequestMapping(method = RequestMethod.GET)
    public String showRegForm(Map model) {
        RegistrationForm regForm = new RegistrationForm();
        model.put("regform", regForm);
        return "regform";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String validateForm(@Valid RegistrationForm regForm, BindingResult result, Map model) {
        if (result.hasErrors()) {
            return "regform";
        }

        model.put("regform", regForm);
        return "regsuccess";
    }
}

Am I applying the constraints incorrectly?

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

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

发布评论

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

评论(1

少女情怀诗 2024-11-12 09:32:04

除了将 添加到您的配置中之外,您还需要确保 JSR-303 jar 位于您的类路径中。来自 文档

[AnnotationDrivenBeanDefinitionParser] ...配置验证器(如果指定),否则默认为由默认 LocalValidatorFactoryBean 创建的新验证器实例如果类路径上存在 JSR-303 API。

In addition to adding <mvc:annotation-driven/> to your config, you need to make sure the JSR-303 jar is on your classpath. From the docs:

[AnnotationDrivenBeanDefinitionParser] ... configures the validator if specified, otherwise defaults to a fresh Validator instance created by the default LocalValidatorFactoryBean if the JSR-303 API is present on the classpath.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文