Spring 版的 JpetStore 中的 AccountFormController 学习札记
Spring 版的 JpetStore 中的 AccountFormController 学习笔记AccountFormController 这个控制器在JpetStor
Spring 版的 JpetStore 中的 AccountFormController 学习笔记
AccountFormController 这个控制器在JpetStore中应该算是比较难的一个控制器,我把我的心得体会记录下来
希望对一同学习JpetStroe的人有些帮助
我下的是spring-framework-2.0.6中自带的 ,如果我有写错或理解错的地方,请大家提醒一下.java 代码?
- public?class?AccountFormController?extends?SimpleFormController?{??
- ????private?Log?log?=?LogFactory.getLog(AccountFormController.class);??
- ??
- ????public?static?final?String[]?LANGUAGES?=?{?"english",?"japanese"?};??
- ??
- ????private?PetStoreFacade?petStore;??
- ??
- ????public?AccountFormController()?{??
- ????????setSessionForm(true);??
- ????????/*?
- ?????????*?当设为false时就是不自动与?Validator?自动绑定,它会通过onBindAndValidate()这个方法?
- ?????????*?去调用如getValidator().validate(account,?errors);?如果我们不调用前面的这个方法的话,?
- ?????????*?那么AccountValidator这个验证器就不会执行,而setValidateOnBinding()这里默认的是true?
- ?????????*?而当这里调用的时候不会调用?Validator中的supports那个方法?
- ?????????*/??
- ????????setValidateOnBinding(false);??
- ????????setCommandName("accountForm");??
- ??
- ????????//?这里设置啦当表单没有验证或者是表单验证没有通过时的?要到指定的*.jsp页面上去??
- ????????setFormView("EditAccountForm");??
- ????????log.info("****???AccountFormController()........\n");??
- ??
- ????}??
- ??
- ????public?void?setPetStore(PetStoreFacade?petStore)?{??
- ????????this.petStore?=?petStore;??
- ????}??
- ??
- ????/*?
- ?????*?当初始化这个类的时候就会?调用这个方法并返回一个AccountForm对象供jsp页面绑定用?
- ????*??再到onBindAndValidate?()去验证,才会调用验证器Validator?
- ?????*/???
- ????protected?Object?formBackingObject(HttpServletRequest?request)??
- ????????????throws?Exception?{??
- ????????UserSession?userSession?=?(UserSession)?WebUtils.getSessionAttribute(??
- ????????????????request,?"userSession");??
- ????????log.info("****???formBackingObject()........\n");?????????
- ????????if?(userSession?!=?null)?{??
- ????????????return?new?AccountForm(this.petStore.getAccount(userSession??
- ????????????????????.getAccount().getUsername()));??
- ????????}?else?{??
- ????????????return?new?AccountForm();??
- ????????}??
- ????}??
- ??
- ????/*?
- ?????*?在这里我们可以在实现啦Validator这个接口验证之前做一些做一些预先的处理?
- ?????*/??
- ????protected?void?onBindAndValidate(HttpServletRequest?request,??
- ????????????Object?command,?BindException?errors)?throws?Exception?{??
- ????????log.info("****??onBindAndValidate()........\n");??
- ??????????
- ????????AccountForm?accountForm?=?(AccountForm)?command;??
- ????????Account?account?=?accountForm.getAccount();??
- ????????if?(request.getParameter("account.listOption")?==?null)?{??
- ????????????account.setListOption(false);??
- ????????}??
- ????????if?(request.getParameter("account.bannerOption")?==?null)?{??
- ????????????account.setBannerOption(false);??
- ????????}??
- ????????/*?
- ?????????*?设置我们将要用验证器去验证的一个对象,而这个对象呢就是AccountForm中的account?
- ?????????*?如果这里不设的话,那么会在AccountValidator中找不到firstName.....这些字段的?
- ?????????*?换句话说就是设一个将要验证的对象的子对象,其实在这里我们不设为account也可以的,?
- ?????????*?如果不设的话,那么在AccountValidator中的?validate(Object?obj,?Errors?errors)?
- ?????????*?中要写成ValidationUtils.rejectIfEmpty(errors,?
- ?????????*?"account.firstName",...);这种形式的,?
- ?????????*?那么在调用的时候传accountForm去就可以啦,如getValidator().validate(accountForm,?
- ?????????*?errors);?
- ?????????*/??
- ????????errors.setNestedPath("account");??
- ??
- ????????getValidator().validate(account,?errors);??
- ????????//?getValidator().validate(accountForm,?errors);??
- ??
- ????????log.info("****?getValidator().validate(account,?errors);........\n");??
- ????????/*?
- ?????????*?这里要把它还原,要不然在下面的ValidationUtils.rejectIfEmpty(errors,?
- ?????????*?"account.username",..);?
- ?????????*?将会出现错误,如果你这里不设回去的话,那么上面这句要改成ValidationUtils.rejectIfEmpty(errors,?
- ?????????*?"username",..);这样也可以.?但是还要在ValidationUtils.rejectIfEmpty(errors,?
- ?????????*?"username",..);这句的下面把它还原,要不然的话它回到?页面显示的时候会出现错误.?
- ?????????*??
- ?????????*/??
- ????????errors.setNestedPath("");??
- ??
- ????????if?(accountForm.isNewAccount())?{??
- ????????????account.setStatus("OK");??
- ????????????ValidationUtils.rejectIfEmpty(errors,?"account.username","USER_ID_REQUIRED",?"User?ID?is?required.");??
- ????????????/*?ValidationUtils.rejectIfEmpty(errors,"username","USER_ID_REQUIRED",?"User?ID?is?required.");?
- ????????????*/??
- ????????????log.info("****?after?ValidationUtils.rejectIfEmpty(errors,?"account.username",..)?........\n");??
- ????????????errors.setNestedPath("");??
- ????????????if?(account.getPassword()?==?null??
- ????????????????????||?account.getPassword().length()?<?1??
- ????????????????????||?!account.getPassword().equals(??
- ????????????????????????????accountForm.getRepeatedPassword()))?{??
- ????????????????errors.reject("PASSWORD_MISMATCH",??
- ????????????????????????????????"Passwords?did?not?match?or?were?not?provided.?Matching?passwords?are?required.");??
- ????????????}??
- ????????}?else?if?(account.getPassword()?!=?null??
- ????????????????&&?account.getPassword().length()?>?0)?{??
- ????????????if?(!account.getPassword().equals(accountForm.getRepeatedPassword()))?{??
- ????????????????errors.reject("PASSWORD_MISMATCH",??
- ????????????????????????????????"Passwords?did?not?match.?Matching?passwords?are?required.");??
- ????????????}??
- ????????}??
- ????}??
- ??
- ????/*?
- ?????*?如果验证通过就不会再调用这个方法,还有就是每次进入这个页面时都会调用一次这个方法,而这个方法在这里就是绑定一些?下拉菜单中的数据?
- ?????*/??
- ????protected?Map?referenceData(HttpServletRequest?request)?throws?Exception?{??
- ????????log.info("****???referenceData()........\n");??
- ????????Map?model?=?new?HashMap();??
- ????????model.put("languages",?LANGUAGES);??
- ????????model.put("categories",?this.petStore.getCategoryList());??
- ????????return?model;??
- ????}??
- ??
- ????//?只有当我们验证通过的时候才会调用这个方法??
- ????protected?ModelAndView?onSubmit(HttpServletRequest?request,??
- ????????????HttpServletResponse?response,?Object?command,?BindException?errors)??
- ????????????throws?Exception?{??
- ????????log.info("****???onSubmit()........\n");??
- ????????AccountForm?accountForm?=?(AccountForm)?command;??
- ??
- ????????try?{??
- ????????????if?(accountForm.isNewAccount())?{??
- ????????????????this.petStore.insertAccount(accountForm.getAccount());??
- ????????????}?else?{??
- ????????????????this.petStore.updateAccount(accountForm.getAccount());??
- ????????????}??
- ????????}?catch?(DataIntegrityViolationException?ex)?{??
- ????????????errors.rejectValue("account.username",?"USER_ID_ALREADY_EXISTS",??
- ????????????????????"User?ID?already?exists:?choose?a?different?ID.");??
- ????????????return?showForm(request,?response,?errors);??
- ????????}??
- ??
- ????????UserSession?userSession?=?new?UserSession(this.petStore??
- ????????????????.getAccount(accountForm.getAccount().getUsername()));??
- ????????PagedListHolder?myList?=?new?PagedListHolder(this.petStore??
- ????????????????.getProductListByCategory(accountForm.getAccount()??
- ????????????????????????.getFavouriteCategoryId()));??
- ????????myList.setPageSize(4);??
- ????????userSession.setMyList(myList);??
- ????????request.getSession().setAttribute("userSession",?userSession);??
- ????????return?super.onSubmit(request,?response,?command,?errors);??
- ????}??
- ??
- }??