推薦淘寶秋冬男裝熱賣(mài)網(wǎng)店

          追求無(wú)止境

          我的程序人生
          隨筆 - 31, 文章 - 2, 評(píng)論 - 20, 引用 - 0
          數(shù)據(jù)加載中……

          Spring MVC 框架學(xué)習(xí)之AbstractFormController以及AbstractFormControll

          在看完BaseCommandController和AbstractCommandController之后,我們?cè)倏碆aseCommandController的另一個(gè)實(shí)現(xiàn)AbstractFormController,以及AbstractFormController的具體實(shí)現(xiàn)SimpleFormController。

          先看看AbstractFormController對(duì)handleRequestInternal的實(shí)現(xiàn):

          protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
          throws Exception {
          // Form submission or new form to show?
          if (isFormSubmission(request)) {
          // Fetch form object from HTTP session, bind, validate, process submission.
          try {
          Object command = getCommand(request);
          ServletRequestDataBinder binder = bindAndValidate(request, command);
          BindException errors = new BindException(binder.getBindingResult());
          return processFormSubmission(request, response, command, errors);
          }
          catch (HttpSessionRequiredException ex) {
          // Cannot submit a session form if no form object is in the session.
          if (logger.isDebugEnabled()) {
          logger.debug("Invalid submit detected: " + ex.getMessage());
          }
          return handleInvalidSubmit(request, response);
          }
          }
          else {
          // New form to show: render form view.
          return showNewForm(request, response);
          }
          }
          這個(gè)方法,首先判斷是不是Form提交,判斷方法是:
          protected boolean isFormSubmission(HttpServletRequest request) {
          return "POST".equals(request.getMethod());
          }
          如果是form提交的話,系統(tǒng)首先創(chuàng)建一個(gè)Command,然后對(duì)數(shù)據(jù)進(jìn)行綁定和驗(yàn)證,之后調(diào)用processFormSubmission方法。showNewForm則調(diào)用showForm。
          在AbstractFormController中里面有兩個(gè)抽象方法:

          protected abstract ModelAndView processFormSubmission(
          HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
          throws Exception;
          protected abstract ModelAndView showForm(
          HttpServletRequest request, HttpServletResponse response, BindException errors)
          throws Exception;



           

           

          好了,看完AbstractFormController之后,再看看SimpleFormController是如何實(shí)現(xiàn):

          protected ModelAndView processFormSubmission(
          HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
          throws Exception {

          if (errors.hasErrors()) {
          if (logger.isDebugEnabled()) {
          logger.debug("Data binding errors: " + errors.getErrorCount());
          }
          return showForm(request, response, errors);
          }
          else if (isFormChangeRequest(request, command)) {
          logger.debug("Detected form change request -> routing request to onFormChange");
          onFormChange(request, response, command, errors);
          return showForm(request, response, errors);
          }
          else {
          logger.debug("No errors -> processing submit");
          return onSubmit(request, response, command, errors);
          }
          }
          在上面的方法中,如果有錯(cuò)誤,調(diào)用showForm,來(lái)顯示form。沒(méi)有錯(cuò)誤的話,則調(diào)用onSubmit方法。
          protected final ModelAndView showForm(
          HttpServletRequest request, BindException errors, String viewName, Map controlModel)
          throws Exception {

          // In session form mode, re-expose form object as HTTP session attribute.
          // Re-binding is necessary for proper state handling in a cluster,
          // to notify other nodes of changes in the form object.
          if (isSessionForm()) {
          String formAttrName = getFormSessionAttributeName(request);
          if (logger.isDebugEnabled()) {
          logger.debug("Setting form session attribute [" + formAttrName + "] to: " + errors.getTarget());
          }
          request.getSession().setAttribute(formAttrName, errors.getTarget());
          }

          // Fetch errors model as starting point, containing form object under
          // "commandName", and corresponding Errors instance under internal key.
          Map model = errors.getModel();

          // Merge reference data into model, if any.
          Map referenceData = referenceData(request, errors.getTarget(), errors);
          if (referenceData != null) {
          model.putAll(referenceData);
          }

          // Merge control attributes into model, if any.
          if (controlModel != null) {
          model.putAll(controlModel);
          }

          // Trigger rendering of the specified view, using the final model.
          return new ModelAndView(viewName, model);
          }
          在showForm中,設(shè)置屬性,放在model中,然后在viewName進(jìn)行設(shè)置。
          FormController就是上面的過(guò)程。具體的執(zhí)行過(guò)程和詳細(xì)信息會(huì)在以后的博客中具體介紹。

          posted on 2009-11-25 17:31 追求無(wú)止境 閱讀(2451) 評(píng)論(0)  編輯  收藏


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 栾川县| 汨罗市| 册亨县| 鄂温| 全州县| 富平县| 凯里市| 莫力| 弥勒县| 营山县| 德清县| 余江县| 包头市| 威信县| 小金县| 大连市| 阜康市| 中牟县| 曲阳县| 昆明市| 奉化市| 大英县| 泽州县| 麻城市| 迭部县| 广灵县| 寿光市| 海丰县| 宜良县| 金沙县| 镇宁| 江山市| 衡东县| 丹凤县| 遵义市| 阳江市| 当涂县| 绥棱县| 景谷| 会宁县| 波密县|