您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

Spring中的WebDataBinder如何使用?

2024/3/13 18:58:13发布30次查看
还是老规矩,开门见山。 我们开发的时候经常会从html,jsp中将参数传到后台,可是经常会遇到的一种情况就是传过来的数据到后台要组装成一种对象的格式,最常见的就是enum类型了。这时候spring提供的@initbinder这个annotation 就发挥了很大的作用。
下面一个例子,我们创建一个javabean(username, password, email and date of birth of a user),我们创建两个自定义的验证类.第一个,我们验证用户名和密码.第二个,验证邮箱,
在eclipse中demo的结构
validator 是一个有两个方法的接口;
boolean supports(class<?> clazz)  :  检验参数是否验证成功的实例类;
 void validate(object target, errors errors)  : 如果   supports() 方法返回真,  target object 合法.  errors.rejectvalue() 方法用一个字段名注册错误信息;
uservalidator.java
 1 package com.concretepage.validators; 2 import org.springframework.stereotype.component; 3 import org.springframework.validation.errors; 4 import org.springframework.validation.validationutils; 5 import org.springframework.validation.validator; 6 import com.concretepage.user; 7 @component 8 public class uservalidator implements validator  { 9     @override10     public boolean supports(class<?> clazz) {11         return user.class.isassignablefrom(clazz);12     }13     @override14     public void validate(object target, errors errors) {15         user user = (user)target;16         validationutils.rejectifemptyorwhitespace(errors, name, ,username is empty);17         validationutils.rejectifemptyorwhitespace(errors, password, , password is empty);18         if (user.getname().length()<5) {19             errors.rejectvalue(name,, username length is less than 5);20         }21     }22 }
uservalidator.java
emailvalidator.java
 1 package com.concretepage.validators; 2 import org.springframework.stereotype.component; 3 import org.springframework.validation.errors; 4 import org.springframework.validation.validationutils; 5 import org.springframework.validation.validator; 6 import com.concretepage.user; 7 @component 8 public class emailvalidator implements validator { 9     @override10     public boolean supports(class<?> clazz) {11         return user.class.isassignablefrom(clazz);12     }13     @override14     public void validate(object target, errors errors) {15         user user = (user)target;16         validationutils.rejectifemptyorwhitespace(errors, email, ,email is empty);17         if (!user.getemail().contains(@)) {18             errors.rejectvalue(email,, email is not valid.);19         }20     }21 }
emailvalidator.java
user.java
 1 package com.concretepage;  2 import java.util.date;  3 public class user {  4     private string name;  5     private string password;  6     private string email;  7     private date dob;  8     public string getname() {  9         return name; 10     } 11     public void setname(string name) { 12         this.name = name; 13     } 14     public string getpassword() { 15         return password; 16     } 17     public void setpassword(string password) { 18         this.password = password; 19     } 20     public string getemail() { 21         return email; 22     } 23     public void setemail(string email) { 24         this.email = email; 25     } 26     public date getdob() { 27         return dob; 28     } 29     public void setdob(date dob) { 30         this.dob = dob; 31     } 32 }
user.java
myworldcontroller
 1 package com.concretepage; 2 import java.text.simpledateformat; 3 import java.util.date; 4  5 import javax.validation.valid; 6  7 import org.springframework.beans.factory.annotation.autowired; 8 import org.springframework.beans.propertyeditors.customdateeditor; 9 import org.springframework.stereotype.controller;10 import org.springframework.ui.modelmap;11 import org.springframework.validation.bindingresult;12 import org.springframework.web.bind.webdatabinder;13 import org.springframework.web.bind.annotation.initbinder;14 import org.springframework.web.bind.annotation.modelattribute;15 import org.springframework.web.bind.annotation.requestmapping;16 import org.springframework.web.bind.annotation.requestmethod;17 import org.springframework.web.servlet.modelandview;18 19 import com.concretepage.validators.emailvalidator;20 import com.concretepage.validators.uservalidator;21 @controller22 @requestmapping(/myworld)23 public class myworldcontroller {24     @autowired25     private uservalidator uservalidator;26     @autowired27     private emailvalidator emailvalidator;28 29     @requestmapping(value=signup, method = requestmethod.get)30     public modelandview user(){31         return new modelandview(user,user,new user());32     }33     @initbinder34     public void databinding(webdatabinder binder) {35         binder.addvalidators(uservalidator, emailvalidator);36         simpledateformat dateformat = new simpledateformat(dd/mm/yyyy);37         dateformat.setlenient(false);38         binder.registercustomeditor(date.class, dob, new customdateeditor(dateformat, true));39     }40     41     @requestmapping(value=save, method = requestmethod.post)42     public string createuser(@modelattribute(user) @valid user user,bindingresult result, modelmap model) {43         if(result.haserrors()) {44             return user;45         }46         system.out.println(name:+ user.getname());47         system.out.println(email:+ user.getemail());48         system.out.println(date of birth:+ user.getdob());49         model.addattribute(msg, welcome to my world!);50         return success;51     }     52 }
myworldcontroller
页面表单
  <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>
以上就是spring中的webdatabinder如何使用?的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product