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");
}
}
}
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.");
}
}
}
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;
}
}
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 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>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有