@RequestMapping(method=RequestMethod.POST,params="order")publicStringcheckout(SessionStatusstatus,@Validated@ModelAttributeOrderorder,BindingResulterrors){if(errors.hasErrors()){return"cart/checkout";}else{this.bookstoreService.store(order);status.setComplete();//remove order from sessionthis.cart.clear();// clear the cartreturn"redirect:/index.htm";}}
packagecom.apress.prospringmvc.bookstore.web.config;importorg.springframework.context.support.ReloadableResourceBundleMessageSource;// Other imports omitted@Configuration@EnableWebMvc@ComponentScan(basePackages={"com.apress.prospringmvc.bookstore.web"})publicclassWebMvcContextConfigurationextendsWebMvcConfigurerAdapter{@BeanpublicMessageSourcemessageSource(){ReloadableResourceBundleMessageSourcemessageSource;messageSource=newReloadableResourceBundleMessageSource();messageSource.setBasename("classpath:/messages");messageSource.setUseCodeAsDefaultMessage(true);returnmessageSource;}}
packagecom.apress.prospringmvc.bookstore.web.config;importorg.springframework.context.MessageSource;importorg.springframework.context.support.ReloadableResourceBundleMessageSource;importorg.springframework.web.servlet.HandlerInterceptor;importorg.springframework.web.servlet.LocaleResolver;importorg.springframework.web.servlet.config.annotation.InterceptorRegistry;importorg.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;importorg.springframework.web.servlet.i18n.CookieLocaleResolver;importorg.springframework.web.servlet.i18n.LocaleChangeInterceptor;// Other imports omitted@Configuration@EnableWebMvc@ComponentScan(basePackages={"com.apress.prospringmvc.bookstore.web"})publicclassWebMvcContextConfigurationextendsWebMvcConfigurerAdapter{@OverridepublicvoidaddInterceptors(InterceptorRegistryregistry){registry.addInterceptor(localeChangeInterceptor());}@BeanpublicHandlerInterceptorlocaleChangeInterceptor(){LocaleChangeInterceptorlocaleChangeInterceptor;localeChangeInterceptor=newLocaleChangeInterceptor();localeChangeInterceptor.setParamName("lang");returnlocaleChangeInterceptor;}@BeanpublicLocaleResolverlocaleResolver(){returnnewCookieLocaleResolver();}@BeanpublicMessageSourcemessageSource(){ReloadableResourceBundleMessageSourcemessageSource;messageSource=newReloadableResourceBundleMessageSource();messageSource.setBasename("classpath:/messages");messageSource.setUseCodeAsDefaultMessage(true);returnmessageSource;}}
packagecom.apress.prospringmvc.bookstore.web.controller;importcom.apress.prospringmvc.bookstore.domain.AccountValidator;importjavax.validation.Valid;// Other imports omitted@Controller@RequestMapping("/customer/register")publicclassRegistrationController{@InitBinderpublicvoidinitBinder(WebDataBinderbinder){binder.setDisallowedFields("id");binder.setValidator(newAccountValidator());}@RequestMapping(method={RequestMethod.POST,RequestMethod.PUT})publicStringhandleRegistration(@Valid@ModelAttributeAccountaccount,BindingResultresult){if(result.hasErrors()){return"customer/register";}this.accountService.save(account);return"redirect:/customer/account/"+account.getId();}// Other methods omitted}
packagecom.apress.prospringmvc.bookstore.domain;importjava.util.Date;importjava.util.List;importjavax.validation.Valid;importorg.hibernate.validator.constraints.Email;importorg.hibernate.validator.constraints.NotEmpty;publicclassAccount{privateLongid;privateStringfirstName;privateStringlastName;privateDatedateOfBirth;@Embedded@ValidprivateAddressaddress=newAddress();@NotEmpty@EmailprivateStringemailAddress;@NotEmptyprivateStringusername;@NotEmptyprivateStringpassword;// getters and setters omitted}