- dongguoh
- 等级: 初级会员
- 性别:
- 文章: 39
- 积分: 80
- 来自: 山水之乡
|
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);
-
-
-
-
-
-
- setValidateOnBinding(false);
- setCommandName("accountForm");
-
-
- setFormView("EditAccountForm");
- log.info("**** AccountFormController()........\n");
-
- }
-
- public void setPetStore(PetStoreFacade petStore) {
- this.petStore = petStore;
- }
-
-
-
-
-
- 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();
- }
- }
-
-
-
-
- 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);
- }
-
-
-
-
-
-
-
-
-
-
- errors.setNestedPath("account");
-
- getValidator().validate(account, errors);
-
-
- log.info("**** getValidator().validate(account, errors);........\n");
-
-
-
-
-
-
-
-
- errors.setNestedPath("");
-
- if (accountForm.isNewAccount()) {
- account.setStatus("OK");
- ValidationUtils.rejectIfEmpty(errors, "account.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);
- }
-
- }
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
返回顶楼 |
|
|