最近在研究spring的jpetstore的例子,新建用户的代码有点迷糊
java 代码
- package org.springframework.samples.jpetstore.web.spring;
-
- import java.util.HashMap;
- import java.util.Map;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- import org.springframework.beans.support.PagedListHolder;
- import org.springframework.dao.DataIntegrityViolationException;
- import org.springframework.samples.jpetstore.domain.Account;
- import org.springframework.samples.jpetstore.domain.logic.PetStoreFacade;
- import org.springframework.validation.BindException;
- import org.springframework.validation.ValidationUtils;
- import org.springframework.web.servlet.ModelAndView;
- import org.springframework.web.servlet.mvc.SimpleFormController;
- import org.springframework.web.util.WebUtils;
-
-
-
-
-
- public class AccountFormController extends SimpleFormController {
-
- public static final String[] LANGUAGES = {"english", "japanese"};
-
- private PetStoreFacade petStore;
-
- public AccountFormController() {
-
-
- System.out.println("AccountFormController() is called!");
- setSessionForm(true);
-
- setValidateOnBinding(false);
-
- setCommandName("accountForm");
-
- setFormView("EditAccountForm");
- }
-
- public void setPetStore(PetStoreFacade petStore) {
- this.petStore = petStore;
- }
-
-
-
- protected Object formBackingObject(HttpServletRequest request) throws Exception {
- System.out.println("formBackingObject is called!");
- UserSession userSession = (UserSession) WebUtils.getSessionAttribute(request, "userSession");
- 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 {
- System.out.println("onBindAndValidate is called!");
- 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);
- errors.setNestedPath("");
-
- if (accountForm.isNewAccount()) {
- account.setStatus("OK");
- ValidationUtils.rejectIfEmpty(errors, "account.username", "USER_ID_REQUIRED", "User ID is required.");
- 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 {
- System.out.println("referenceData is called!");
- 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 {
- System.out.println("onSubmit is called!");
- 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);
- }
-
- }
因为simpleForm既显示表单又处理表单,所以我在代码中加入了输出语句来看哪些方法是在显示表单时调用,哪些是在处理表单时调用。答案我是找到了,注释中也标注了,但是还是觉得有点迷糊,referenceData()方法在显示表单时被调用的作用是什么?errors.setNestedPath("account");是 什么意思?感觉好像在这个处理上没有struts的清晰。手头上的 资料对于simpleForm方面的没有这个复杂。文档嘛也似乎看得不是很明白。
java 代码
- public AccountFormController() {
-
-
- System.out.println("AccountFormController() is called!");
- setSessionForm(true);
-
- setValidateOnBinding(false);
-
- setCommandName("accountForm");
-
- setFormView("EditAccountForm");
- }
上面这段代码能猜出是什么意思,但 好像没怎么彻底理解。setSessionForm(true);什么时候把表单放到Session中的??
setValidateOnBinding(false);什么时候绑定的验证的呢?setCommandName("accountForm");accountForm是个含有Accout属性和isNewAccount的bean。怎么把表单数据填入accountForm的呢?什么时候填入的呢?
对于流程网上有以下解释:
它的处理流程是这样的:
get请求来到时,这样处理:
a) 请求传递给一个controller对象
b) 调用formBackingObject()方法,创建一个command对象的实例。 c) 调用initBinder(),注册需要的类型转换器
d) 调用showForm()方法,返回准备呈现给用户的视图
e) 调用referenceData()方法,准备给用户显示相关的数据。如用户登录需要选择的年度信息
f) 返回formView指定的视图
post请求来到时,这样处理:
a) 调用formBackingObject()方法,创建一个command对象的实例。
b) 将请求传来的参数写入command对象
c) 如果设置为要求验证,则调用validator类进行数据验证
d) 调用onBindAndValidate()方法,该方法允许自定义数据绑定和校验处理
e) 调用onSubmit()方法,进行业务逻辑处理
哪位大侠能针对这个代码详细的解释下究竟这个 代码 的处理流程是什么呢?在显示表单时细节是什么?在处理表单时细节是什么?上面代码各个方法是具体意思是什么呢?什么时候调用的?做什么用的呢?