Java SpringMvc如何根据用户执行的功能为对象使用不同的验证器
我有一个对象名为Offices,我想根据用户想要执行的功能执行不同类型的验证,例如,当一个官员记录正在注册/保存时,我希望执行检查,如果其为空,则生成一个官员编号;当一个记录正在更新时,我不希望不执行此检查并执行更新语句 然而,从那时起,我在实现这一目标方面遇到了困难。我研究过不同的方法,但它不够干净或灵活。我尝试过以下方法,面临的问题是:Java SpringMvc如何根据用户执行的功能为对象使用不同的验证器,java,spring,spring-mvc,validation,Java,Spring,Spring Mvc,Validation,我有一个对象名为Offices,我想根据用户想要执行的功能执行不同类型的验证,例如,当一个官员记录正在注册/保存时,我希望执行检查,如果其为空,则生成一个官员编号;当一个记录正在更新时,我不希望不执行此检查并执行更新语句 然而,从那时起,我在实现这一目标方面遇到了困难。我研究过不同的方法,但它不够干净或灵活。我尝试过以下方法,面临的问题是: 将注册的验证器与控制器一起使用,但是每个控制器只允许注册一个验证器。这使得验证的实现适用于控制器中执行的所有功能 使用验证器Facade可以允许整个应用程序
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");
}
..............
}
公务员注册验证类
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");
}
..............
}
需要对要更新的记录使用不同类型的验证
@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获取按钮值update或save,并将其包含在验证器中,以决定是验证更新还是保存。在我寻找最干净、最好的方法之前,有没有人做过类似的事情。到目前为止,我已经决定使用HttpServletRequest.getParameter(“操作”);我发现这种方法有点陈旧,不干净 您不需要在
WebDataBinder
中注册验证器。相反,您可以为每个需求创建两个(或任意数量的)不同的验证程序类。比如说
public class OfficerRegistrationValidation implements Validator {...}
public class OfficerUpdateValidation implements Validator {...}
使用@Component
或
声明为每个组件创建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
注释。您的验证器
实例将针对特定用例执行自定义验证。我不认为对同一类使用多个验证器是正确的做法。无论执行何种操作,对象的验证都应相同。生成新的客户编号
的场景应该通过服务层中的保存
方法得到更好的处理。