Struts (4) of the SSH framework -- Struts check for missing BeanUtils in Struts1
In the previous blog, Struts (3) in the SSH framework-core method of Struts running process, we mentioned that processPopulate () in RequestProcessor is used to fill data for ActionForm, how does it put form data into an ActionForm? -- A third-party tool, BeanUtils, is a very important service used to operate javaBean.
public static void populate( Object bean, String prefix, String suffix, HttpServletRequest request) throws ServletException { // Build a list of relevant request parameters from this request HashMap properties = new HashMap(); // Iterator of parameter names Enumeration names = null; // Map for multipart parameters Map multipartParameters = null; String contentType = request.getContentType(); String method = request.getMethod(); boolean isMultipart = false; if (bean instanceof ActionForm) { ((ActionForm) bean).setMultipartRequestHandler(null); } MultipartRequestHandler multipartHandler = null; if ((contentType != null) && (contentType.startsWith("multipart/form-data")) && (method.equalsIgnoreCase("POST"))) { // Get the ActionServletWrapper from the form bean ActionServletWrapper servlet; if (bean instanceof ActionForm) { servlet = ((ActionForm) bean).getServletWrapper(); } else { throw new ServletException( "bean that's supposed to be " + "populated from a multipart request is not of type " + "\"org.apache.struts.action.ActionForm\", but type " + "\"" + bean.getClass().getName() + "\""); } // Obtain a MultipartRequestHandler multipartHandler = getMultipartHandler(request); if (multipartHandler != null) { isMultipart = true; // Set servlet and mapping info servlet.setServletFor(multipartHandler); multipartHandler.setMapping( (ActionMapping) request.getAttribute(Globals.MAPPING_KEY)); // Initialize multipart request class handler multipartHandler.handleRequest(request); //stop here if the maximum length has been exceeded Boolean maxLengthExceeded = (Boolean) request.getAttribute( MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED); if ((maxLengthExceeded != null) && (maxLengthExceeded.booleanValue())) { ((ActionForm) bean).setMultipartRequestHandler(multipartHandler); return; } //retrieve form values and put into properties multipartParameters = getAllParametersForMultipartRequest( request, multipartHandler); names = Collections.enumeration(multipartParameters.keySet()); } } if (!isMultipart) { names = request.getParameterNames(); } while (names.hasMoreElements()) { String name = (String) names.nextElement(); String stripped = name; if (prefix != null) { if (!stripped.startsWith(prefix)) { continue; } stripped = stripped.substring(prefix.length()); } if (suffix != null) { if (!stripped.endsWith(suffix)) { continue; } stripped = stripped.substring(0, stripped.length() - suffix.length()); } Object parameterValue = null; if (isMultipart) { parameterValue = multipartParameters.get(name); } else { parameterValue = request.getParameterValues(name); } // Populate parameters, except "standard" struts attributes // such as 'org.apache.struts.action.CANCEL' if (!(stripped.startsWith("org.apache.struts."))) { properties.put(stripped, parameterValue); } } // Set the corresponding properties of our bean try { BeanUtils.populate(bean, properties); } catch(Exception e) { throw new ServletException("BeanUtils.populate", e); } finally { if (multipartHandler != null) { // Set the multipart request handler for our ActionForm. // If the bean isn't an ActionForm, an exception would have been // thrown earlier, so it's safe to assume that our bean is // in fact an ActionForm. ((ActionForm) bean).setMultipartRequestHandler(multipartHandler); } } }
The first half of this implementation is about the upload code. If you use the upload function, you can carefully read the code in the following sections. We will not explain it here, so let's look at it.
if (!isMultipart) { names = request.getParameterNames(); }
This is used to get all the names of the form and prepare for the Copy and conversion of the attribute values.
while (names.hasMoreElements()) { String name = (String) names.nextElement(); String stripped = name; if (prefix != null) { if (!stripped.startsWith(prefix)) { continue; } stripped = stripped.substring(prefix.length()); } if (suffix != null) { if (!stripped.endsWith(suffix)) { continue; } stripped = stripped.substring(0, stripped.length() - suffix.length()); } Object parameterValue = null; if (isMultipart) { parameterValue = multipartParameters.get(name); } else { parameterValue = request.getParameterValues(name); } }// Populate parameters, except "standard" struts attributes // such as 'org.apache.struts.action.CANCEL' if (!(stripped.startsWith("org.apache.struts."))) { properties.put(stripped, parameterValue); }
Traverse all the names of the form and obtain the value corresponding to the name through parameterValue = request. getParameterValues (name. Properties. put (stripped, parameterValue); put the name as the key and the value corresponding to the name as the value into the map.
// Set the corresponding properties of our bean try { BeanUtils.populate(bean, properties); } catch(Exception e) { throw new ServletException("BeanUtils.populate", e); } finally { if (multipartHandler != null) { // Set the multipart request handler for our ActionForm. // If the bean isn't an ActionForm, an exception would have been // thrown earlier, so it's safe to assume that our bean is // in fact an ActionForm. ((ActionForm) bean).setMultipartRequestHandler(multipartHandler); } } }
Here, the third-party service BeanUtils is called to convert and assign values to attribute values. This method will traverse the type of the ActionForm value, and change the type of the value in Map to the type corresponding to ActionForm. The processPopulate method is implemented here. The BeanUtils populate method maps the names and values obtained from the form to the corresponding ActionForm. The source code is as follows:
public static void populate(Object bean, Map properties) throws IllegalAccessException, InvocationTargetException { // Do nothing unless both arguments have been specified if ((bean == null) || (properties == null)) { return; } if (log.isDebugEnabled()) { log.debug("BeanUtils.populate(" + bean + ", " + properties + ")"); } // Loop through the property name/value pairs to be set Iterator names = properties.keySet().iterator(); while (names.hasNext()) { // Identify the property name and value(s) to be assigned String name = (String) names.next(); if (name == null) { continue; } Object value = properties.get(name); // Perform the assignment for this property setProperty(bean, name, value); } }
BeanUtils. populate (bean, properties); after execution, the actioning from form data to ActionForm is completed.
BeanUtils is a third-party service used to operate javaBean. BeanUtils is introduced here. The next article describes and learns BeanUtils through instances.