首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Spring 版的 JpetStore 中的 AccountFormController 学习札记

2012-10-26 
Spring 版的 JpetStore 中的 AccountFormController 学习笔记AccountFormController 这个控制器在JpetStor

Spring 版的 JpetStore 中的 AccountFormController 学习笔记
AccountFormController 这个控制器在JpetStore中应该算是比较难的一个控制器,我把我的心得体会记录下来
希望对一同学习JpetStroe的人有些帮助
我下的是spring-framework-2.0.6中自带的 ,如果我有写错或理解错的地方,请大家提醒一下.java 代码?

  1. public?class?AccountFormController?extends?SimpleFormController?{??
  2. ????private?Log?log?=?LogFactory.getLog(AccountFormController.class);??
  3. ??
  4. ????public?static?final?String[]?LANGUAGES?=?{?"english",?"japanese"?};??
  5. ??
  6. ????private?PetStoreFacade?petStore;??
  7. ??
  8. ????public?AccountFormController()?{??
  9. ????????setSessionForm(true);??
  10. ????????/*?
  11. ?????????*?当设为false时就是不自动与?Validator?自动绑定,它会通过onBindAndValidate()这个方法?
  12. ?????????*?去调用如getValidator().validate(account,?errors);?如果我们不调用前面的这个方法的话,?
  13. ?????????*?那么AccountValidator这个验证器就不会执行,而setValidateOnBinding()这里默认的是true?
  14. ?????????*?而当这里调用的时候不会调用?Validator中的supports那个方法?
  15. ?????????*/??
  16. ????????setValidateOnBinding(false);??
  17. ????????setCommandName("accountForm");??
  18. ??
  19. ????????//?这里设置啦当表单没有验证或者是表单验证没有通过时的?要到指定的*.jsp页面上去??
  20. ????????setFormView("EditAccountForm");??
  21. ????????log.info("****???AccountFormController()........\n");??
  22. ??
  23. ????}??
  24. ??
  25. ????public?void?setPetStore(PetStoreFacade?petStore)?{??
  26. ????????this.petStore?=?petStore;??
  27. ????}??
  28. ??
  29. ????/*?
  30. ?????*?当初始化这个类的时候就会?调用这个方法并返回一个AccountForm对象供jsp页面绑定用?
  31. ????*??再到onBindAndValidate?()去验证,才会调用验证器Validator?
  32. ?????*/???
  33. ????protected?Object?formBackingObject(HttpServletRequest?request)??
  34. ????????????throws?Exception?{??
  35. ????????UserSession?userSession?=?(UserSession)?WebUtils.getSessionAttribute(??
  36. ????????????????request,?"userSession");??
  37. ????????log.info("****???formBackingObject()........\n");?????????
  38. ????????if?(userSession?!=?null)?{??
  39. ????????????return?new?AccountForm(this.petStore.getAccount(userSession??
  40. ????????????????????.getAccount().getUsername()));??
  41. ????????}?else?{??
  42. ????????????return?new?AccountForm();??
  43. ????????}??
  44. ????}??
  45. ??
  46. ????/*?
  47. ?????*?在这里我们可以在实现啦Validator这个接口验证之前做一些做一些预先的处理?
  48. ?????*/??
  49. ????protected?void?onBindAndValidate(HttpServletRequest?request,??
  50. ????????????Object?command,?BindException?errors)?throws?Exception?{??
  51. ????????log.info("****??onBindAndValidate()........\n");??
  52. ??????????
  53. ????????AccountForm?accountForm?=?(AccountForm)?command;??
  54. ????????Account?account?=?accountForm.getAccount();??
  55. ????????if?(request.getParameter("account.listOption")?==?null)?{??
  56. ????????????account.setListOption(false);??
  57. ????????}??
  58. ????????if?(request.getParameter("account.bannerOption")?==?null)?{??
  59. ????????????account.setBannerOption(false);??
  60. ????????}??
  61. ????????/*?
  62. ?????????*?设置我们将要用验证器去验证的一个对象,而这个对象呢就是AccountForm中的account?
  63. ?????????*?如果这里不设的话,那么会在AccountValidator中找不到firstName.....这些字段的?
  64. ?????????*?换句话说就是设一个将要验证的对象的子对象,其实在这里我们不设为account也可以的,?
  65. ?????????*?如果不设的话,那么在AccountValidator中的?validate(Object?obj,?Errors?errors)?
  66. ?????????*?中要写成ValidationUtils.rejectIfEmpty(errors,?
  67. ?????????*?"account.firstName",...);这种形式的,?
  68. ?????????*?那么在调用的时候传accountForm去就可以啦,如getValidator().validate(accountForm,?
  69. ?????????*?errors);?
  70. ?????????*/??
  71. ????????errors.setNestedPath("account");??
  72. ??
  73. ????????getValidator().validate(account,?errors);??
  74. ????????//?getValidator().validate(accountForm,?errors);??
  75. ??
  76. ????????log.info("****?getValidator().validate(account,?errors);........\n");??
  77. ????????/*?
  78. ?????????*?这里要把它还原,要不然在下面的ValidationUtils.rejectIfEmpty(errors,?
  79. ?????????*?"account.username",..);?
  80. ?????????*?将会出现错误,如果你这里不设回去的话,那么上面这句要改成ValidationUtils.rejectIfEmpty(errors,?
  81. ?????????*?"username",..);这样也可以.?但是还要在ValidationUtils.rejectIfEmpty(errors,?
  82. ?????????*?"username",..);这句的下面把它还原,要不然的话它回到?页面显示的时候会出现错误.?
  83. ?????????*??
  84. ?????????*/??
  85. ????????errors.setNestedPath("");??
  86. ??
  87. ????????if?(accountForm.isNewAccount())?{??
  88. ????????????account.setStatus("OK");??
  89. ????????????ValidationUtils.rejectIfEmpty(errors,?"account.username","USER_ID_REQUIRED",?"User?ID?is?required.");??
  90. ????????????/*?ValidationUtils.rejectIfEmpty(errors,"username","USER_ID_REQUIRED",?"User?ID?is?required.");?
  91. ????????????*/??
  92. ????????????log.info("****?after?ValidationUtils.rejectIfEmpty(errors,?"account.username",..)?........\n");??
  93. ????????????errors.setNestedPath("");??
  94. ????????????if?(account.getPassword()?==?null??
  95. ????????????????????||?account.getPassword().length()?<?1??
  96. ????????????????????||?!account.getPassword().equals(??
  97. ????????????????????????????accountForm.getRepeatedPassword()))?{??
  98. ????????????????errors.reject("PASSWORD_MISMATCH",??
  99. ????????????????????????????????"Passwords?did?not?match?or?were?not?provided.?Matching?passwords?are?required.");??
  100. ????????????}??
  101. ????????}?else?if?(account.getPassword()?!=?null??
  102. ????????????????&&?account.getPassword().length()?>?0)?{??
  103. ????????????if?(!account.getPassword().equals(accountForm.getRepeatedPassword()))?{??
  104. ????????????????errors.reject("PASSWORD_MISMATCH",??
  105. ????????????????????????????????"Passwords?did?not?match.?Matching?passwords?are?required.");??
  106. ????????????}??
  107. ????????}??
  108. ????}??
  109. ??
  110. ????/*?
  111. ?????*?如果验证通过就不会再调用这个方法,还有就是每次进入这个页面时都会调用一次这个方法,而这个方法在这里就是绑定一些?下拉菜单中的数据?
  112. ?????*/??
  113. ????protected?Map?referenceData(HttpServletRequest?request)?throws?Exception?{??
  114. ????????log.info("****???referenceData()........\n");??
  115. ????????Map?model?=?new?HashMap();??
  116. ????????model.put("languages",?LANGUAGES);??
  117. ????????model.put("categories",?this.petStore.getCategoryList());??
  118. ????????return?model;??
  119. ????}??
  120. ??
  121. ????//?只有当我们验证通过的时候才会调用这个方法??
  122. ????protected?ModelAndView?onSubmit(HttpServletRequest?request,??
  123. ????????????HttpServletResponse?response,?Object?command,?BindException?errors)??
  124. ????????????throws?Exception?{??
  125. ????????log.info("****???onSubmit()........\n");??
  126. ????????AccountForm?accountForm?=?(AccountForm)?command;??
  127. ??
  128. ????????try?{??
  129. ????????????if?(accountForm.isNewAccount())?{??
  130. ????????????????this.petStore.insertAccount(accountForm.getAccount());??
  131. ????????????}?else?{??
  132. ????????????????this.petStore.updateAccount(accountForm.getAccount());??
  133. ????????????}??
  134. ????????}?catch?(DataIntegrityViolationException?ex)?{??
  135. ????????????errors.rejectValue("account.username",?"USER_ID_ALREADY_EXISTS",??
  136. ????????????????????"User?ID?already?exists:?choose?a?different?ID.");??
  137. ????????????return?showForm(request,?response,?errors);??
  138. ????????}??
  139. ??
  140. ????????UserSession?userSession?=?new?UserSession(this.petStore??
  141. ????????????????.getAccount(accountForm.getAccount().getUsername()));??
  142. ????????PagedListHolder?myList?=?new?PagedListHolder(this.petStore??
  143. ????????????????.getProductListByCategory(accountForm.getAccount()??
  144. ????????????????????????.getFavouriteCategoryId()));??
  145. ????????myList.setPageSize(4);??
  146. ????????userSession.setMyList(myList);??
  147. ????????request.getSession().setAttribute("userSession",?userSession);??
  148. ????????return?super.onSubmit(request,?response,?command,?errors);??
  149. ????}??
  150. ??
  151. }??

热点排行