欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Spring中WebDataBinder使用详解

程序员文章站 2023-12-13 15:52:16
spring mvc validator @initbinder and webdatabinder;validator是一个用来我们自定义验证的sping接口,webda...

spring mvc validator @initbinder and webdatabinder;validator是一个用来我们自定义验证的sping接口,webdatabinder 绑定你的自定义参数,你直接在你的控制器类中通过@initbinder 注解的方式配置 web 数据绑定.registercustomeditor()是一个属性编辑器,比如自定义的日期编辑它绑定web请求参数到javabean的属性;

下面一个例子,我们创建一个javabean(username, password, email and date of birth of a user),我们创建两个自定义的验证类.第一个,我们验证用户名和密码.第二个,验证邮箱,

在eclipse中demo的结构

Spring中WebDataBinder使用详解

Spring中WebDataBinder使用详解

validator 是一个有两个方法的接口;

boolean supports(class<?> clazz) : 检验参数是否验证成功的实例类;

void validate(object target, errors errors) : 如果 supports() 方法返回真,  target object 合法. errors.rejectvalue() 方法用一个字段名注册错误信息;

uservalidator.java

package com.concretepage.validators;
import org.springframework.stereotype.component;
import org.springframework.validation.errors;
import org.springframework.validation.validationutils;
import org.springframework.validation.validator;
import com.concretepage.user;
@component
public class uservalidator implements validator {
  @override
  public boolean supports(class<?> clazz) {
    return user.class.isassignablefrom(clazz);
  }
  @override
  public void validate(object target, errors errors) {
    user user = (user)target;
    validationutils.rejectifemptyorwhitespace(errors, "name", "","username is empty");
    validationutils.rejectifemptyorwhitespace(errors, "password", "", "password is empty");
    if (user.getname().length()<5) {
      errors.rejectvalue("name","", "username length is less than 5");
    }
  }
} 

emailvalidator.java

package com.concretepage.validators;
import org.springframework.stereotype.component;
import org.springframework.validation.errors;
import org.springframework.validation.validationutils;
import org.springframework.validation.validator;
import com.concretepage.user;
@component
public class emailvalidator implements validator {
  @override
  public boolean supports(class<?> clazz) {
    return user.class.isassignablefrom(clazz);
  }
  @override
  public void validate(object target, errors errors) {
    user user = (user)target;
    validationutils.rejectifemptyorwhitespace(errors, "email", "","email is empty");
    if (!user.getemail().contains("@")) {
      errors.rejectvalue("email","", "email is not valid.");
    }
  }
} 

user.java

package com.concretepage;
import java.util.date;
public class user {
  private string name;
  private string password;
  private string email;
  private date dob;
  public string getname() {
    return name;
  }
  public void setname(string name) {
    this.name = name;
  }
  public string getpassword() {
    return password;
  }
  public void setpassword(string password) {
    this.password = password;
  }
  public string getemail() {
    return email;
  }
  public void setemail(string email) {
    this.email = email;
  }
  public date getdob() {
    return dob;
  }
  public void setdob(date dob) {
    this.dob = dob;
  }
} 

myworldcontroller

package com.concretepage;
import java.text.simpledateformat;
import java.util.date;

import javax.validation.valid;

import org.springframework.beans.factory.annotation.autowired;
import org.springframework.beans.propertyeditors.customdateeditor;
import org.springframework.stereotype.controller;
import org.springframework.ui.modelmap;
import org.springframework.validation.bindingresult;
import org.springframework.web.bind.webdatabinder;
import org.springframework.web.bind.annotation.initbinder;
import org.springframework.web.bind.annotation.modelattribute;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.requestmethod;
import org.springframework.web.servlet.modelandview;

import com.concretepage.validators.emailvalidator;
import com.concretepage.validators.uservalidator;
@controller
@requestmapping("/myworld")
public class myworldcontroller {
  @autowired
  private uservalidator uservalidator;
  @autowired
  private emailvalidator emailvalidator;

  @requestmapping(value="signup", method = requestmethod.get)
  public modelandview user(){
    return new modelandview("user","user",new user());
  }
  @initbinder
  public void databinding(webdatabinder binder) {
    binder.addvalidators(uservalidator, emailvalidator);
    simpledateformat dateformat = new simpledateformat("dd/mm/yyyy");
    dateformat.setlenient(false);
    binder.registercustomeditor(date.class, "dob", new customdateeditor(dateformat, true));
  }
  
  @requestmapping(value="save", method = requestmethod.post)
  public string createuser(@modelattribute("user") @valid user user,bindingresult result, modelmap model) {
    if(result.haserrors()) {
      return "user";
    }
    system.out.println("name:"+ user.getname());
    system.out.println("email:"+ user.getemail());
    system.out.println("date of birth:"+ user.getdob());
    model.addattribute("msg", "welcome to my world!");
    return "success";
  }  
}

form页面表单

<form:form action="save" method="post" commandname="user">
  <tr> <td>user name:</td> <td><form:input path="name"/> </td> 
     <td> <form:errors path="name" cssstyle="color: red;"/></td> </tr>
  <tr> <td> password :</td> <td><form:input path="password"/> </td> 
     <td> <form:errors path="password" cssstyle="color: red;"/> </td> </tr>
  <tr> <td> email :</td> <td><form:input path="email"/> </td> 
     <td> <form:errors path="email" cssstyle="color: red;"/> </td> </tr>
  <tr> <td> date of birth :</td> <td><form:input path="dob"/> </td> 
     <td> <form:errors path="dob" cssstyle="color: red;"/> </td> </tr>     
  <tr> <td colspan=3>  <input type="submit"> </td>
 </form:form>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: