Spring MVC Validator @InitBinder and WebDataBinder;Validator是一個(gè)用來我們自定義驗(yàn)證的sping接口,WebDataBinder 綁定你的自定義參數(shù),你直接在你的控制器類中通過@InitBinder 注解的方式配置 Web 數(shù)據(jù)綁定.registerCustomEditor()是一個(gè)屬性編輯器,比如自定義的日期編輯它綁定web請(qǐng)求參數(shù)到JavaBean的屬性;
創(chuàng)新互聯(lián)制作網(wǎng)站網(wǎng)頁找三站合一網(wǎng)站制作公司,專注于網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì)制作、做網(wǎng)站,網(wǎng)站設(shè)計(jì),企業(yè)網(wǎng)站搭建,網(wǎng)站開發(fā),建網(wǎng)站業(yè)務(wù),680元做網(wǎng)站,已為上1000+服務(wù),創(chuàng)新互聯(lián)網(wǎng)站建設(shè)將一如既往的為我們的客戶提供最優(yōu)質(zhì)的網(wǎng)站建設(shè)、網(wǎng)絡(luò)營(yíng)銷推廣服務(wù)!
下面一個(gè)例子,我們創(chuàng)建一個(gè)JavaBean(username, password, email and date of birth of a user),我們創(chuàng)建兩個(gè)自定義的驗(yàn)證類.第一個(gè),我們驗(yàn)證用戶名和密碼.第二個(gè),驗(yàn)證郵箱,
在Eclipse中Demo的結(jié)構(gòu)
Validator 是一個(gè)有兩個(gè)方法的接口;
boolean supports(Class<?> clazz) : 檢驗(yàn)參數(shù)是否驗(yàn)證成功的實(shí)例類;
void validate(Object target, Errors errors) : 如果 supports() 方法返回真, target object 合法. Errors.rejectValue() 方法用一個(gè)字段名注冊(cè)錯(cuò)誤信息;
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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
文章名稱:Spring中WebDataBinder使用詳解
文章URL:http://jinyejixie.com/article16/jjpggg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、網(wǎng)站營(yíng)銷、虛擬主機(jī)、App設(shè)計(jì)、Google、標(biāo)簽優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)