推薦淘寶秋冬男裝熱賣(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ú)止境 閱讀(2459) 評(píng)論(0)  編輯  收藏


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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 滨州市| 潼南县| 涡阳县| 图片| 家居| 峡江县| 伽师县| 凤阳县| 华阴市| 吐鲁番市| 瑞金市| 宜丰县| 马尔康县| 绿春县| 上虞市| 肃宁县| 遂昌县| 鹿泉市| 轮台县| 江安县| 抚远县| 安达市| 镇雄县| 青岛市| 清徐县| 石门县| 呼和浩特市| 高台县| 台山市| 沭阳县| 宝鸡市| 瓦房店市| 昭平县| 子长县| 阳原县| 大安市| 蛟河市| 哈尔滨市| 松溪县| 南汇区| 漯河市|