SpringMvc如何基于用户执行的功能对对象使用不同的验证器
问题内容:
我有一个称为高级管理人员的对象,我想根据用户想要执行的功能来执行不同类型的验证,例如,当高级管理人员记录正在注册/保存时,我想执行检查是否为空并生成高级管理人员的方法数字,并且当记录正在更新时,我不想不执行此检查并执行更新语句。
但是,自那时以来,我在实现此目标方面遇到问题。我研究了不同的方法,它不够干净或不够灵活。我尝试了以下方法,面临的问题是;
-
但是,将已注册的验证程序与控制器一起使用时,每个控制器仅允许注册一个验证程序。这使得该验证的实现适用于控制器中执行的所有功能。
- 使用Validator Facade可以为整个应用程序提供一个验证类,但是它根据对象的实例类型选择验证,这将每个对象的验证器数量限制为一个(需要更正)。
无需对方法使用单独的Controller,如何对同一对象执行不同的验证。
班主任
public class Officers implements Serializable{
private String userName;
private String password;
private String password2;
private String fName;
private String lName;
private String oName;
private int divisionNo;
private officerNumber;
人员注册验证类别
@Component
public class OfficerRegistrationValidation implements Validator {
public boolean supports(Class<?> clazz) {
return Officers.class.equals(clazz);
}
public void validate(Object target, Errors errors) {
Officers officer = (Officers) target;
if (officer.getPassword() == null) {
errors.rejectValue("password", "password.required");
}
if (officer.getPassword2() == null) {
errors.rejectValue("password2", "password2.required");
}
..............
}
控制者
@Controller
public class OfficerController {
@InitBinder("officers")
protected void initBinder(WebDataBinder binder){
//removes white spaces
binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
//formats date
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
//By passing true this will convert empty strings to null
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
dateFormat.setLenient(false);
//binder.setValidator(new OfficerRegistrationValidation());
binder.setValidator(officerRegistrationValidation);
}
@RequestMapping(value="officer_registration_save.htm", method = RequestMethod.POST)
public ModelAndView loadPage(HttpServletRequest request,HttpServletResponse response,
@ModelAttribute Officers officer,BindingResult result,ModelMap m, Model model) throws Exception {
if(result.hasErrors()){
return new ModelAndView("officer_registration");
}else
doSave();
}
需要使用其他类型的验证来更新记录
@RequestMapping(value="officer_registration_update.htm", method = RequestMethod.POST)
public ModelAndView loadPage(HttpServletRequest request,HttpServletResponse response,
@ModelAttribute Officers officer,BindingResult result,ModelMap m, Model model) throws Exception {
if(result.hasErrors()){
return new ModelAndView("officer_registration");
}else
doSave();
}
我最终使用的方法是通过HttpServletRequest获取按钮值更新或保存,并将其包含在验证器中以决定是否验证更新还是保存。在我寻找他最干净,最好的方法之前,有没有人做过类似的事情。到目前为止,我已经决定使用HttpServletRequest请求request.getParameter(“
action”); 我发现这种方法有些陈旧且不干净。
问题答案:
您无需在中注册验证器WebDataBinder
。相反,您可以Validator
为每个需求创建两个(或任意数量)不同的类。例如
public class OfficerRegistrationValidation implements Validator {...}
public class OfficerUpdateValidation implements Validator {...}
使用@Component
或<bean>
声明为每个容器创建bean 。将它们注入您的@Controller
班级
@Controller
public class OfficerController {
@Inject
private OfficerRegistrationValidation officerRegistrationValidation;
@Inject
private OfficerUpdateValidation officerUpdateValidation;
然后在每种方法中使用所需的特定方法
@RequestMapping(method = RequestMethod.POST)
public /* or other return type */ String registerOfficer(@Valid @ModelAttribute Officer officer, BindingResult errors /*, more parameters */) {
officerRegistrationValidation.validate(officer, errors);
if (errors.hasErrors()) {
...// do something
}
...// return something
}
请勿在中注册任何一个WebDataBinder
。@Valid
将执行默认验证,例如@NotEmpty
或@Pattern
注释。您的Validator
实例将针对特定用例执行自定义验证。