<i id='lKBie'><tr id='lKBie'><dt id='lKBie'><q id='lKBie'><span id='lKBie'><b id='lKBie'><form id='lKBie'><ins id='lKBie'></ins><ul id='lKBie'></ul><sub id='lKBie'></sub></form><legend id='lKBie'></legend><bdo id='lKBie'><pre id='lKBie'><center id='lKBie'></center></pre></bdo></b><th id='lKBie'></th></span></q></dt></tr></i><div id='lKBie'><tfoot id='lKBie'></tfoot><dl id='lKBie'><fieldset id='lKBie'></fieldset></dl></div>
    • <bdo id='lKBie'></bdo><ul id='lKBie'></ul>

    <legend id='lKBie'><style id='lKBie'><dir id='lKBie'><q id='lKBie'></q></dir></style></legend>

      <small id='lKBie'></small><noframes id='lKBie'>

        <tfoot id='lKBie'></tfoot>

        javax.validation.constraints 中的注释不起作用

        时间:2023-07-26

        1. <small id='vsf4j'></small><noframes id='vsf4j'>

            <bdo id='vsf4j'></bdo><ul id='vsf4j'></ul>
          • <i id='vsf4j'><tr id='vsf4j'><dt id='vsf4j'><q id='vsf4j'><span id='vsf4j'><b id='vsf4j'><form id='vsf4j'><ins id='vsf4j'></ins><ul id='vsf4j'></ul><sub id='vsf4j'></sub></form><legend id='vsf4j'></legend><bdo id='vsf4j'><pre id='vsf4j'><center id='vsf4j'></center></pre></bdo></b><th id='vsf4j'></th></span></q></dt></tr></i><div id='vsf4j'><tfoot id='vsf4j'></tfoot><dl id='vsf4j'><fieldset id='vsf4j'></fieldset></dl></div>

              1. <tfoot id='vsf4j'></tfoot>
                <legend id='vsf4j'><style id='vsf4j'><dir id='vsf4j'><q id='vsf4j'></q></dir></style></legend>
                  <tbody id='vsf4j'></tbody>

                  本文介绍了javax.validation.constraints 中的注释不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  使用 javax.validation.constraints 中的注解(如 @Size@NotNull 等)需要什么配置?这是我的代码:

                  What configuration is needed to use annotations from javax.validation.constraints like @Size, @NotNull, etc.? Here's my code:

                  import javax.validation.constraints.NotNull;
                  import javax.validation.constraints.Size;
                  
                  public class Person {
                        @NotNull
                        private String id;
                  
                        @Size(max = 3)
                        private String name;
                  
                        private int age;
                  
                        public Person(String id, String name, int age) {
                          this.id = id;
                          this.name = name;
                          this.age = age;
                        }
                  }
                  

                  当我尝试在另一个类中使用它时,验证不起作用(即对象创建时没有错误):

                  When I try to use it in another class, validation doesn't work (i.e. the object is created without error):

                  Person P = new Person(null, "Richard3", 8229));
                  

                  为什么这不对 idname 应用约束?我还需要做什么?

                  Why doesn't this apply constraints for id and name? What else do I need to do?

                  推荐答案

                  要让 JSR-303 bean 验证在 Spring 中工作,您需要做几件事:

                  For JSR-303 bean validation to work in Spring, you need several things:

                  1. 注解的MVC命名空间配置:<mvc:annotation-driven/>
                  2. JSR-303 规范 JAR:validation-api-1.0.0.GA.jar(看起来你已经有了)
                  3. 规范的实现,例如 Hibernate Validation,这似乎是最常用的示例:hibernate-validator-4.1.0.Final.jar
                  4. 在要验证的 bean 中,验证注释,来自规范 JAR 或来自实现 JAR(您已经完成)
                  5. 在您要验证的处理程序中,使用@Valid 注释您要验证的对象,然后在方法签名中包含一个BindingResult 以捕获错误.
                  1. MVC namespace configuration for annotations: <mvc:annotation-driven />
                  2. The JSR-303 spec JAR: validation-api-1.0.0.GA.jar (looks like you already have that)
                  3. An implementation of the spec, such as Hibernate Validation, which appears to be the most commonly used example: hibernate-validator-4.1.0.Final.jar
                  4. In the bean to be validated, validation annotations, either from the spec JAR or from the implementation JAR (which you have already done)
                  5. In the handler you want to validate, annotate the object you want to validate with @Valid, and then include a BindingResult in the method signature to capture errors.

                  例子:

                  @RequestMapping("handler.do")
                  public String myHandler(@Valid @ModelAttribute("form") SomeFormBean myForm, BindingResult result, Model model) {
                      if(result.hasErrors()) {
                        ...your error handling...
                      } else {
                        ...your non-error handling....
                      }
                  }
                  

                  这篇关于javax.validation.constraints 中的注释不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何使用@ComponentScan 注解扫描多个路径? 下一篇:Java 寻找具有特定注解的方法及其注解元素

                  相关文章

                  最新文章

                  • <bdo id='3cJEw'></bdo><ul id='3cJEw'></ul>

                  <small id='3cJEw'></small><noframes id='3cJEw'>

                  <i id='3cJEw'><tr id='3cJEw'><dt id='3cJEw'><q id='3cJEw'><span id='3cJEw'><b id='3cJEw'><form id='3cJEw'><ins id='3cJEw'></ins><ul id='3cJEw'></ul><sub id='3cJEw'></sub></form><legend id='3cJEw'></legend><bdo id='3cJEw'><pre id='3cJEw'><center id='3cJEw'></center></pre></bdo></b><th id='3cJEw'></th></span></q></dt></tr></i><div id='3cJEw'><tfoot id='3cJEw'></tfoot><dl id='3cJEw'><fieldset id='3cJEw'></fieldset></dl></div>
                  1. <tfoot id='3cJEw'></tfoot>

                      <legend id='3cJEw'><style id='3cJEw'><dir id='3cJEw'><q id='3cJEw'></q></dir></style></legend>