弹簧验证拒绝值

时间:2018-08-09 11:50:47

标签: java spring spring-mvc

我需要验证field1并根据约4到5个约束抛出多条错误消息。

    @Controller
    public class MyController {

        @RequestMapping(method = RequestMethod.POST, value = "myPage.html")
        public void myHandler(MyForm myForm, BindingResult result,@ valid 
        ModelAttribute("fields") Fields fields) {

          if(somethingtotrue){
                result.rejectValue(field1, desc, "field1 not valid");
          }else if(aboveIffalse){
          result.rejectValue(field1, desc, "field1 length  not valid");
          }else if(aboveiffalse){
          result.rejectValue(field1, desc, "field1 does not conatin special 
               char  valid");
          }else{
            result.rejectValue(field1, desc, "field1 not valid");
          }


        }
    }

要验证的数据类field1

        public class Fields{
                private String field1;
                private String field2;
                private String field3;
                private String field4;
                //getters and setters
        }

0 个答案:

没有答案