ActionForm中的setter方法

ActionServlet中的一个方法processActionForm,当我们在截取字符串,再根据字符串取得ActionMapping之后,我们就要用利用ActionMapping来创建ActionForm,并且把ActionForm放到request或session中管理。获得ActionForm之后,我们就要将ActionForm中的数据放到Mapping中,以便实例化Action。在Struts中有一个方法是专门把ActionForm的数据放到Mapping的,这个方法就是processPopulate。今天我们就来详细来看看这个方法。

       首先这个方法主要的功能是将表单数据放到Map中,并且将Map的值根据ActionForm类型转换好后设置到ActionForm中。

       这个方法具体的流程是首先执行ActionForm中的reset方法进行重置,然后得到表单中所有输入域的name名称,再调用request.getParameterValues(),根据name名称得到相应的值,最后将表单中的数据全部放到一个map中,map的key为表单输入域的名称,map的value为表单输入域的值(字符串数组),接下来调用一个第三方组件BeanUtils,将Map中的值,根据ActionForm中的类型先转换好,再调用ActionForm中的setter方法设置到ActionForm上。

     下面咱们来跟随源代码来看看这个方法的实现过程.首先还是和以前博客一样设置断点,进入process方法,找到processPopulate方法:

 

       进入这个方法,看到这个方法的实现源代码:

  1. protectedvoid processPopulate(HttpServletRequest request,  
  2.   
  3.                                    HttpServletResponse response,  
  4.   
  5.                                    ActionForm form,  
  6.   
  7.                                    ActionMapping mapping)  
  8.   
  9.         throws ServletException {  
  10.   
  11.         if (form == null) {  
  12.   
  13.             return;  
  14.   
  15.         }  
  16.         // Populate the bean properties of this ActionForm instance   
  17.   
  18.         if (log.isDebugEnabled()) {  
  19.   
  20.             log.debug(" Populating bean properties from this request");  
  21.   
  22.         }  
  23.         form.setServlet(this.servlet);  
  24.   
  25.         form.reset(mapping, request);  
  26.         if (mapping.getMultipartClass() != null) {  
  27.   
  28.             request.setAttribute(Globals.MULTIPART_KEY,  
  29.   
  30.                                  mapping.getMultipartClass());  
  31.   
  32.         }  
  33.   
  34.         RequestUtils.populate(form, mapping.getPrefix(), mapping.getSuffix(),  
  35.   
  36.                               request);  
  37.         // Set the cancellation request attribute if appropriate   
  38.   
  39.         if ((request.getParameter(Constants.CANCEL_PROPERTY) != null) ||  
  40.   
  41.             (request.getParameter(Constants.CANCEL_PROPERTY_X) != null)) {         
  42.             request.setAttribute(Globals.CANCEL_KEY, Boolean.TRUE);  
  43.   
  44.         }   
  45. }  

       其中,form.reset(mapping, request);这个方法就是讲form重置,作用是使ActionForm中的值恢复初始状态。

      下面RequestUtils.populate(form, mapping.getPrefix(), mapping.getSuffix()这个方法就是要完成填充map和转换类型等操作的,具体实现:

  1. publicstaticvoid populate(  
  2.   
  3.             Object bean,  
  4.   
  5.             String prefix,  
  6.   
  7.             String suffix,  
  8.   
  9.             HttpServletRequest request)  
  10.   
  11.             throws ServletException {  
  12.   
  13.         // Build a list of relevant request parameters from this request   
  14.         HashMap properties = new HashMap();  
  15.         // Iterator of parameter names   
  16.         Enumeration names = null;  
  17.   
  18.         // Map for multipart parameters   
  19.   
  20.         Map multipartParameters = null;  
  21.   
  22.          String contentType = request.getContentType();  
  23.   
  24.         String method = request.getMethod();  
  25.   
  26.         boolean isMultipart = false;  
  27.   
  28.          if (bean instanceof ActionForm) {  
  29.   
  30.             ((ActionForm) bean).setMultipartRequestHandler(null);  
  31.   
  32.         }  
  33.   
  34.     MultipartRequestHandler multipartHandler = null;  
  35.   
  36.         if ((contentType != null)  
  37.   
  38.                 && (contentType.startsWith("multipart/form-data"))  
  39.   
  40.                 && (method.equalsIgnoreCase("POST"))) {  
  41.   
  42.             // Get the ActionServletWrapper from the form bean   
  43.   
  44.             ActionServletWrapper servlet;  
  45.   
  46.             if (bean instanceof ActionForm) {  
  47.   
  48.                 servlet = ((ActionForm) bean).getServletWrapper();  
  49.   
  50.             } else {  
  51.   
  52.                 thrownew ServletException(  
  53.   
  54.                         "bean that's supposed to be "  
  55.   
  56.                         + "populated from a multipart request is not of type "  
  57.   
  58.                         + "\"org.apache.struts.action.ActionForm\", but type "  
  59.   
  60.                         + "\""  
  61.   
  62.                         + bean.getClass().getName()  
  63.   
  64.                         + "\"");  
  65.   
  66.             }  
  67.   
  68.              // Obtain a MultipartRequestHandler   
  69.   
  70.             multipartHandler = getMultipartHandler(request);  
  71.   
  72.              if (multipartHandler != null) {  
  73.   
  74.                 isMultipart = true;  
  75.   
  76.                 // Set servlet and mapping info   
  77.   
  78.                 servlet.setServletFor(multipartHandler);  
  79.   
  80.                 multipartHandler.setMapping(  
  81.   
  82.                         (ActionMapping) request.getAttribute(Globals.MAPPING_KEY));  
  83.   
  84.                 // Initialize multipart request class handler   
  85.   
  86.                 multipartHandler.handleRequest(request);  
  87.   
  88.                 //stop here if the maximum length has been exceeded   
  89.   
  90.                 Boolean maxLengthExceeded =  
  91.   
  92.                         (Boolean) request.getAttribute(  
  93.   
  94.                                 MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED);  
  95.   
  96.                 if ((maxLengthExceeded != null) && (maxLengthExceeded.booleanValue())) {  
  97.   
  98.                     ((ActionForm) bean).setMultipartRequestHandler(multipartHandler);  
  99.   
  100.                     return;  
  101.   
  102.                 }  
  103.   
  104.                 //retrieve form values and put into properties   
  105.   
  106.                 multipartParameters = getAllParametersForMultipartRequest(  
  107.   
  108.                         request, multipartHandler);  
  109.   
  110.                 names = Collections.enumeration(multipartParameters.keySet());  
  111.   
  112.             }  
  113.   
  114.         }  
  115.   
  116.         if (!isMultipart) {  
  117.   
  118.             names = request.getParameterNames();  
  119.   
  120.         }  
  121.         while (names.hasMoreElements()) {  
  122.   
  123.             String name = (String) names.nextElement();  
  124.   
  125.             String stripped = name;  
  126.   
  127.             if (prefix != null) {  
  128.   
  129.                 if (!stripped.startsWith(prefix)) {  
  130.   
  131.                     continue;  
  132.   
  133.                 }  
  134.   
  135.                 stripped = stripped.substring(prefix.length());  
  136.   
  137.             }  
  138.   
  139.             if (suffix != null) {  
  140.   
  141.                 if (!stripped.endsWith(suffix)) {  
  142.   
  143.                     continue;  
  144.   
  145.                 }  
  146.   
  147.                 stripped = stripped.substring(0, stripped.length() - suffix.length());  
  148.   
  149.             }  
  150.   
  151.             Object parameterValue = null;  
  152.   
  153.             if (isMultipart) {  
  154.   
  155.                 parameterValue = multipartParameters.get(name);  
  156.   
  157.             } else {  
  158.   
  159.                 parameterValue = request.getParameterValues(name);  
  160.   
  161.             }  
  162.             // Populate parameters, except "standard" struts attributes   
  163.   
  164.             // such as 'org.apache.struts.action.CANCEL'   
  165.   
  166.             if (!(stripped.startsWith("org.apache.struts."))) {  
  167.   
  168.                 properties.put(stripped, parameterValue);  
  169.   
  170.             }  
  171.   
  172.         }  
  173.   
  174.         // Set the corresponding properties of our bean   
  175.   
  176.         try {  
  177.   
  178.             BeanUtils.populate(bean, properties);  
  179.   
  180.         } catch(Exception e) {  
  181.   
  182.             thrownew ServletException("BeanUtils.populate", e);  
  183.   
  184.         } finally {  
  185.   
  186.             if (multipartHandler != null) {  
  187.   
  188.                 // Set the multipart request handler for our ActionForm.   
  189.   
  190.                 // If the bean isn't an ActionForm, an exception would have been   
  191.   
  192.                 // thrown earlier, so it's safe to assume that our bean is   
  193.   
  194.                 // in fact an ActionForm.   
  195.   
  196.                 ((ActionForm) bean).setMultipartRequestHandler(multipartHandler);  
  197.   
  198.             }  
  199.   
  200.         }  
  201.     }  

     这段实现的前半部分是关于上传的代码,因为咱们这个实例不和上传有关,所以直接忽略,直接到

  1. if (!isMultipart) {  
  2.   
  3.             names = request.getParameterNames();  
  4.   
  5.         }  

     这段代码,这段代码主要是获得表单的所有名称,之后通过下面这段代码:

  1. while (names.hasMoreElements()) {  
  2.   
  3.             String name = (String) names.nextElement();  
  4.   
  5.             String stripped = name;  
  6.   
  7.             if (prefix != null) {  
  8.   
  9.                 if (!stripped.startsWith(prefix)) {  
  10.   
  11.                     continue;  
  12.   
  13.                 }  
  14.   
  15.                 stripped = stripped.substring(prefix.length());  
  16.   
  17.             }  
  18.   
  19.             if (suffix != null) {  
  20.   
  21.                 if (!stripped.endsWith(suffix)) {  
  22.   
  23.                     continue;  
  24.   
  25.                 }  
  26.   
  27.                 stripped = stripped.substring(0, stripped.length() - suffix.length());  
  28.   
  29.             }  
  30.   
  31.             Object parameterValue = null;  
  32.   
  33.             if (isMultipart) {  
  34.   
  35.                 parameterValue = multipartParameters.get(name);  
  36.   
  37.             } else {  
  38.   
  39.                 parameterValue = request.getParameterValues(name);  
  40.   
  41.             }  
  42.   
  43.              // Populate parameters, except "standard" struts attributes   
  44.   
  45.             // such as 'org.apache.struts.action.CANCEL'   
  46.   
  47.             if (!(stripped.startsWith("org.apache.struts."))) {  
  48.   
  49.                 properties.put(stripped, parameterValue);  
  50.   
  51.             }  
  52.   
  53.         }  

      遍历名称,并且通过parameterValue = request.getParameterValues(name);获得名称对应的value值,之后通过

  1. if (!(stripped.startsWith("org.apache.struts."))) {  
  2.   
  3.                 properties.put(stripped, parameterValue);  
  4.   
  5.             }  

      将名称作为key值,讲名称的value值作为value值添加到map中,到此为止,我们就讲表单数据添加到了map中。

随后,调用第三方的组件来实现类型转换:

  1. try {  
  2.   
  3.             BeanUtils.populate(bean, properties);  
  4.   
  5.         } catch(Exception e) {  
  6.   
  7.             thrownew ServletException("BeanUtils.populate", e);  
  8.   
  9.         } finally {  
  10.   
  11.             if (multipartHandler != null) {  
  12.   
  13.                 // Set the multipart request handler for our ActionForm.   
  14.   
  15.                 // If the bean isn't an ActionForm, an exception would have been   
  16.   
  17.                 // thrown earlier, so it's safe to assume that our bean is   
  18.   
  19.                 // in fact an ActionForm.   
  20.   
  21.                 ((ActionForm) bean).setMultipartRequestHandler(multipartHandler);  
  22.   
  23.             }  
  24.   
  25.         }  

     这个方法会遍历ActionForm的值的类型,并且讲Map中的值的类型改为和ActionForm对应的类型。

posted @ 2012-05-14 22:43  hibernate3例子  阅读(423)  评论(0编辑  收藏  举报