InitBinder和数据校验

public class MyDateeditor extends PropertiesEditor{
@Override
public void setAsText(String text) throws IllegalArgumentException {
SimpleDateFormat sdf=getDate(text);
Date da=null;
try {
da=sdf.parse(text);
} catch (ParseException e) {
e.printStackTrace();
}
setValue(da);
}

private SimpleDateFormat getDate(String text) {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");
if (Pattern.matches("^\d{4}-\d{1,2}-\d{1,2}$",text)){
sdf=new SimpleDateFormat("yyyy-MM-dd");
}
return sdf;
}

}


@Controller
public class FirstController {
@InitBinder
public void initbinder(WebDataBinder binder){
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
binder.registerCustomEditor(Date.class,new MyDateeditor());
}



@RequestMapping("first")
public String login(String username, int age, Date birthday){
System.out.println(username);
System.out.println(age);
System.out.println(birthday);
return "second";
}
}



配置:
<context:component-scan base-package="day011"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>



数据校验:

public class ScondController {
@RequestMapping("/register")
public String register(@Valid User info, BindingResult br, Model model){
if (br.getErrorCount()>0){
FieldError userage=br.getFieldError("uage");
FieldError username=br.getFieldError("uame");
FieldError userphone=br.getFieldError("phone");
FieldError birthday=br.getFieldError("birthday");
FieldError email=br.getFieldError("email");
if (userage!=null){
String useragemsg=userage.getDefaultMessage();
model.addAttribute("useragemsg",useragemsg);
}
if (username!=null){
String usernamemsg=username.getDefaultMessage();
model.addAttribute("usernamemsg",usernamemsg);
}
if (userphone!=null){
String userphonemsg=userphone.getDefaultMessage();
model.addAttribute("userphonemsg",userphonemsg);
}
if (birthday!=null){
String birthdaymsg=birthday.getDefaultMessage();
model.addAttribute("birthdaymsg",birthdaymsg);
}
if (email!=null){
String emailmsg=email.getDefaultMessage();
model.addAttribute("emailmsg",emailmsg);
}
}
return "second";
}
}



public class User {
@NotNull(message = "用户名不能为空")
private String uame;
@Min(value = 10 ,message = "你太小啦")
@Max(value = 50,message = "你太大啦")
private Integer uage;

@DateTimeFormat(style = "yyyy-MM-dd")
private Date birthday;

@Pattern(regexp = "^\w+@\w+\.\w+$",message = "邮箱格式不正确")
private String email;

public String getUame() {
return uame;
}

public void setUame(String uame) {
this.uame = uame;
}

public Integer getUage() {
return uage;
}

public void setUage(Integer uage) {
this.uage = uage;
}

public Date getBirthday() {
return birthday;
}

public void setBirthday(Date birthday) {
this.birthday = birthday;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone;
}

@Pattern(regexp = "1[3|5|7|9|8]\d{9}",message = "手机号格式不正确")
private String phone;


}



<context:component-scan base-package="day012"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>

<bean id="myb" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="providerClass" value="org.hibernate.validator.HibernateValidator"></property>
</bean>
<mvc:annotation-driven validator="myb"></mvc:annotation-driven>


原文地址:https://www.cnblogs.com/xu06123/p/8693539.html