zoukankan      html  css  js  c++  java
  • [springmvc] Validation

    Trigger validation by marking a JavaBean parameter as @Valid

      The JavaBean will be passed to a Validator for validation

      The JavaBean will be passed to a Validator for validation

    Binding and validation errors can be trapped and introspected by declaring a BindingResult parameter

      Must follow the JavaBean parameter in the method signature

      Errors automatically exported in the model when rendering views

      Not supported with other request parameter types (@RequestBody, etc)

        @RequestMapping("/validate")
    public @ResponseBody String validate(@Valid JavaBean bean, BindingResult result) {
    if (result.hasErrors()) {
    return "Object has validation errors";
    } else {
    return "No errors";
    }
    }

    JavaBean.java

    import java.util.Date;

    import javax.validation.constraints.Future;
    import javax.validation.constraints.Max;
    import javax.validation.constraints.NotNull;

    import org.springframework.format.annotation.DateTimeFormat;
    import org.springframework.format.annotation.DateTimeFormat.ISO;

    public class JavaBean {

    @NotNull
    @Max(5)
    private Integer number;

    @NotNull
    @Future
    @DateTimeFormat(iso=ISO.DATE)
    private Date date;

    public Integer getNumber() {
    return number;
    }

    public void setNumber(Integer number) {
    this.number = number;
    }

    public Date getDate() {
    return date;
    }

    public void setDate(Date date) {
    this.date = date;
    }

    }




      

  • 相关阅读:
    [PTA练习] 愿天下有情人都是失散多年的兄妹(25分)
    sql server远程连接非1433端口
    java把double转化为long型
    StringUtils工具类
    JfreeChart折线图
    Log4j配置
    Ibatis,Spring整合(注解方式注入)
    Spring中的autowire属性(转)
    MyBatis3入门样例
    struts2 ibatis Spring系统架构图
  • 原文地址:https://www.cnblogs.com/lavieenrose/p/2418801.html
Copyright © 2011-2022 走看看