4

我有一个名为官员的对象,我想根据用户想要执行的功能执行不同类型的验证,例如,当正在注册/保存官员记录时,我想检查其是否为 NULL 并生成官员编号,当更新记录时,我不想不执行此检查并执行更新语句。

但是,从那以后我在实现这一目标时遇到了问题。我看过不同的方法,但它不够干净或不够灵活。我尝试了以下方法,面临的问题是;

  1. 将注册验证器与控制器一起使用,但是每个控制器只允许注册一个验证器。这使得该验证的实现适用于控制器中执行的所有功能。

    1. 使用验证器外观可以允许整个应用程序使用一个验证类,但是它根据对象的实例类型选择验证,这将每个对象的验证器数量限制为一个(待纠正)。

如何在不为方法使用单独的控制器的情况下对同一对象执行不同的验证。

班主任

   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;

OfficeRegistrationValidation 类

    @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"); 我发现这种方法有点陈旧而且不干净。

4

1 回答 1

7

您无需在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将执行默认验证,例如,for@NotEmpty@Patternannotations。您的Validator实例将为特定用例执行自定义验证。

于 2013-10-27T22:54:43.213 回答