4

私は役員と呼ばれるオブジェクトを持っています。ユーザーが実行したい機能に基づいて、さまざまなタイプの検証を実行したいと考えています。番号とレコードが更新されているときに、このチェックを実行せずに更新ステートメントを実行したくありません。

しかし、それ以来、これを達成するのに問題があります。私はさまざまなアプローチを見てきましたが、十分にクリーンでも柔軟でもありません。私は次のアプローチを試しましたが、直面した問題は次のとおりです。

  1. コントローラーで登録済みバリデーターを使用しますが、各コントローラーで登録できるバリデーターは 1 つだけです。これにより、その検証の実装がコントローラーで実行されるすべての関数に適用されます。

    1. Validator Facade を使用すると、アプリケーション全体に対して 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;

OfficerRegistrationValidation クラス

    @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