有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java所有验证消息都可以在带注释的驱动hibernate验证中看到

我使用hibernate注释通过spring表单验证验证字段

这是我的模型课:

     public class NewPasword {

      @NotEmpty(message="Please Provide Password")
      @Pattern(regexp = "^(?=.*?[A-Z])(?=.*?[0-9])(?=\\S+$).{8,}$", message = "{encryptedpassword.required}")
      @Size(min=8, max=19)   
      private String newPassword;

       ........

        .......
      }

这是我的JSP

  .....
    <div class="row">
                <form:input path="field" />
                <form:error path="field" />
    </div>
 .....

执行时,所有验证消息都显示在前端,不应显示

有谁能建议我如何在up之前只获得一条验证消息

例如:如果字段为空,则消息应仅为“请输入字段”

若字段与模式不匹配,则消息应仅为“请输入有效字段”

如果字段为&;lt 8和&;gt 19则信息应仅为“长度应在8-19之间”

如果需要更多信息,请询问


共 (1) 个答案

  1. # 1 楼答案

    你可以用GroupGroupSequence来解决这个问题

    @GroupSequence({ Step1.class, Step2.class, Step3.class })
    public class NewPasword {
    
      public interface Step1 {}
      public interface Step2 {}
      public interface Step3 {}
    
      @NotEmpty(message="Please Provide Password",groups = Step1.class)
      @Pattern(regexp = "^(?=.*?[A-Z])(?=.*?[0-9])(?=\\S+$).{8,}$", message = "{encryptedpassword.required}", groups = Step2.class)
      @Size(min=8, max=19, groups = Step3.class)   
      private String newPassword;
    
      }
    

    参见参考资料:Grouping