還是舊法矩,開門見山。 我們開發的時候常常會從html,jsp中將參數傳到後台,可是常會遇到的一種情況就是傳過來的資料到後台要組裝成一種物件的格式,最常見的就是enum類型了。這時候spring提供的@initBinder這個annotation 就發揮了很大的作用。
下面一個範例,我們建立一個JavaBean(username, password, email and date of birth of a user),我們建立兩個自訂的驗證類.第一個,我們驗證使用者名稱和密碼.第二個,驗證郵箱,
在Eclipse中Demo的結構





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()


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


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> <tr> <td>User Name:</td> <td> <input> </td> <td> <errors></errors> </td> </tr> <tr> <td> Password :</td> <td> <input> </td> <td> <errors></errors> </td> </tr> <tr> <td> Email :</td> <td> <input> </td> <td> <errors></errors> </td> </tr> <tr> <td> Date of Birth :</td> <td> <input> </td> <td> <errors></errors> </td> </tr> <tr> <td> <input> </td> <p> </p> </tr> </form>
以上是Spring中的WebDataBinder如何使用?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

java实现定时任务Jdk自带的库中,有两种方式可以实现定时任务,一种是Timer,另一种是ScheduledThreadPoolExecutor。Timer+TimerTask创建一个Timer就创建了一个线程,可以用来调度TimerTask任务Timer有四个构造方法,可以指定Timer线程的名字以及是否设置为为守护线程。默认名字Timer-编号,默认不是守护线程。主要有三个比较重要的方法:cancel():终止任务调度,取消当前调度的所有任务,正在运行的任务不受影响purge():从任务队

一、@RequestParam注解对应的axios传参方法以下面的这段Springjava代码为例,接口使用POST协议,需要接受的参数分别是tsCode、indexCols、table。针对这个Spring的HTTP接口,axios该如何传参?有几种方法?我们来一一介绍。@PostMapping("/line")publicList

SpringBoot和SpringCloud都是SpringFramework的扩展,它们可以帮助开发人员更快地构建和部署微服务应用程序,但它们各自有不同的用途和功能。SpringBoot是一个快速构建Java应用的框架,使得开发人员可以更快地创建和部署基于Spring的应用程序。它提供了一个简单、易于理解的方式来构建独立的、可执行的Spring应用

随着技术的更新迭代,Java5.0开始支持注解。而作为java中的领军框架spring,自从更新了2.5版本之后也开始慢慢舍弃xml配置,更多使用注解来控制spring框架。

作为一名Java开发者,学习和使用Spring框架已经是一项必不可少的技能。而随着云计算和微服务的盛行,学习和使用SpringCloud成为了另一个必须要掌握的技能。SpringCloud是一个基于SpringBoot的用于快速构建分布式系统的开发工具集。它为开发者提供了一系列的组件,包括服务注册与发现、配置中心、负载均衡和断路器等,使得开发者在构建微

1.Spring项目的创建1.1创建Maven项目第一步,创建Maven项目,Spring也是基于Maven的。1.2添加spring依赖第二步,在Maven项目中添加Spring的支持(spring-context,spring-beans)在pom.xml文件添加依赖项。org.springframeworkspring-context5.2.3.RELEASEorg.springframeworkspring-beans5.2.3.RELEASE刷新等待加载完成。1.3创建启动类第三步,创

SpringBean的生命周期管理一、SpringBean的生命周期通过以下方式来指定Bean的初始化和销毁方法,当Bean为单例时,Bean归Spring容器管理,Spring容器关闭,就会调用Bean的销毁方法当Bean为多例时,Bean不归Spring容器管理,Spring容器关闭,不会调用Bean的销毁方法二、通过@Bean的参数(initMethod,destroyMethod)指定Bean的初始化和销毁方法1、项目结构2、PersonpublicclassPerson{publicP

spring设计模式有:1、依赖注入和控制反转;2、工厂模式;3、模板模式;4、观察者模式;5、装饰者模式;6、单例模式;7、策略模式和适配器模式等。详细介绍:1、依赖注入和控制反转: 这两个设计模式是Spring框架的核心。通过依赖注入,Spring负责管理和注入组件之间的依赖关系,降低了组件之间的耦合度。控制反转则是指将对象的创建和依赖关系的管理交给Spring容器等等。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

禪工作室 13.0.1
強大的PHP整合開發環境