使用InitBinder做验证的情况一般会在此Controller中提交的数据需要有一些是业务性质的,也即比较复杂的验证情况下才会使用。大部份简单的表单验证,使用annotation验证即可以解决。
这里需要注意的一点:InitBinder和Annotation两种验证只能二选一,如果使用了InitBinder,就不能使用Annotation验证。
前面的web.xml和spring.xml的配置就不再重复,可参见上面链接中的配置。一模一样。
直接上代码:
1、User5 model实体类
package com.my.controller.bean;
import java.util.Date;
public class User5 {
private long id;
private String name;
private String password;
private Date createTime;
private int age;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
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 Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
2、新增一个Validator:
package com.my.controller.validator;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
import com.my.controller.bean.User5;
@Component
public class TestValidator implements Validator {
@Override
public boolean supports(Class> paramClass) {
return User5.class.equals(paramClass);
}
@Override
public void validate(Object obj, Errors errors) {
User5 user = (User5) obj;
ValidationUtils.rejectIfEmptyOrWhitespace(errors, “name”, “valid.name”, null, “”);
if(user.getAge() < 18) {
errors.rejectValue(“age”, “valid.ageMin”, new Object[]{“age” ,18}, “年龄不能小于{1}岁”);
}
}
}
这里需要加入@Component,注入DI
3、Controller
package com.my.controller;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.Validator;
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.my.controller.bean.User5;
@Controller
@RequestMapping(value=”binder”)
public class TestInitBinderController {
@Autowired
@Qualifier(value=”testValidator”)
private Validator validator;
@InitBinder
private void initBinder(WebDataBinder binder) {
binder.setValidator(validator);
}
@RequestMapping(method=RequestMethod.GET)
public String index() {
return “/TestInitBinder/index”;
}
@RequestMapping(value=”add”, method=RequestMethod.POST)
public ModelAndView add(@ModelAttribute @Valid User5 user, BindingResult result) {
ModelAndView view = new ModelAndView(“TestInitBinder/index”);
view.addObject(“user”, user);
if(result.hasErrors()) {
List errs = result.getFieldErrors();
Map mapErrors = new LinkedHashMap();
for(FieldError err : errs) {
System.out.println(“ObjectName:” + err.getObjectName() + “\tFieldName:” + err.getField()
+ “\tFieldValue:” + err.getRejectedValue() + “\tMessage:” + err.getDefaultMessage());
mapErrors.put(err.getField(), err.getDefaultMessage());
view.addObject(“errors”, mapErrors);
}
return view;
}
return view;
}
}
把Validator注入到Controller中。
事实上,使用InitBinder,在add controller中的err.getDefaultMessage()方法是取不到对应正确的message的。可以看最后的输入打印结果。
4、View
Init binder
User name:
Password:
Age:
Error:
注意,这里只能使用来取得错误信息,且,这个一定要在
当中。
5、结果测试
点击Add button:
打印输出:
可以看到,这里取不到错误的正确信息
事实上,在一个非常复杂表单页面,里头所提交的数据验证有一定的业务逻辑性时,InitBinder应该都不多用,因为很多时候我们可以使用一个Map,把errors插入进去,在页面读取即可。比如:
Map errors;
errors.add(“name”, “user name can NOT be empty!”);
:
:
页面中只需要使用:
${errors.name}
即可。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。