SSH框架之Struts(4)——Struts查漏补缺BeanUtils在Struts1中
来源:互联网 发布:java 审批流 开源项目 编辑:程序博客网 时间:2024/05/16 12:19
在上篇博客SSH框架之Struts(3)——Struts的运行流程之核心方法,我们提到RequestProcessor中的processPopulate()是用来为为ActionForm 填充数据,它是怎么实现将表单数据放入到一个ActionForm中的呢?——第三方工具,BeanUtils,相对来说,这是一个非常重要的用来操作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); } } }
这段实现的前半部分是关于上传的代码,如果用到上传功能的可以仔细阅读以下前边部分的代码,我们这里不做解释,可以往下看。
if (!isMultipart) { names = request.getParameterNames(); }
这是用来获取到表单所有的名称,进而为属性值的Copy和转换做准备
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); }
遍历表单所有的名称,并通过parameterValue = request.getParameterValues(name);获得名称对应的value值。之后就是通过properties.put(stripped, parameterValue);将名称作为key,名称对应的值作为value放入到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); } } }
在这里,主要是调用第三方服务BeanUtils来实现属性值的转换和赋值。这个方法会遍历ActionForm的值的类型,并且讲Map中的值的类型改为和ActionForm对应的类型。到这里processPopulate的方法就实现完毕。BeanUtils的populate方法是将从form表单取到的名称和值映射到对应的ActionForm中,源码如下
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);执行完,至此,完成了form表单数据到ActionForm的映射。
BeanUtils作为一个操作javaBean的第三方服务,这里引出BeanUtils,下篇通过实例来了解和学习BeanUtils。
- SSH框架之Struts(4)——Struts查漏补缺BeanUtils在Struts1中
- SSH框架之Struts(4)——Struts查漏补缺BeanUtils的简单使用
- Struts1—从BeanUtils看struts的实现原理3(自定义BeanUtils的转换器)
- SSH:Struts1框架(采用struts上传文件)
- SSH框架之Struts(2)——Struts的运行流程之配置文件
- SSH框架之Struts(3)——Struts的运行流程之核心方法
- 自学SSH框架之---struts(三):源码分析struts1如何实现Action单例模式
- 自学SSH框架之---struts(四):struts1数据验证方面简介
- 【SSH之旅】一步步学习Struts1框架(二):Struts实例
- SSH框架之Struts(1)——Struts的运行流程
- 自学SSH框架之---struts(一):struts的配置
- Struts1——从BeanUtils看struts的实现原理1
- Struts1——从BeanUtils看struts的实现原理2
- 自学SSH框架之---struts(二):struts如何接受表单数据,并在处理后转发出去
- SSH框架——整合Demo(Struts、Spring、Hibernate)
- Struts2——SSH (Spring+Struts+Hibernate)框架搭建之配置文件序列一
- Spring——SSH (Spring+Struts+Hibernate)框架搭建之配置文件序列二
- Spring beans——SSH (Spring+Struts+Hibernate)框架搭建之配置文件序列三
- java mina TcpSessionManager
- QT4中构建多线程的服务器and QT'Socket
- docker 私有hub搭建及使用
- IT瘾 IT社区推荐资讯
- java mina TcpSocketAddress
- SSH框架之Struts(4)——Struts查漏补缺BeanUtils在Struts1中
- CRC24校验算法--Java源码
- 谷歌海外推广_Google海外推广怎么做?
- android.util.ArrayMap 解读
- PHP为什么会被认为是草根语言?
- HTML5基础扩展——地理位置、本地存储、缓存
- 估计技术宅干的
- Android将程序崩溃信息保存本地文件发送至服务器
- .net CHARTING图表控件免费下载地址