在上篇博客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中的更多相关文章

  1. Entity Framework 查漏补缺 (一)

    明确EF建立的数据库和对象之间的关系 EF也是一种ORM技术框架, 将对象模型和关系型数据库的数据结构对应起来,开发人员不在利用sql去操作数据相关结构和数据.以下是EF建立的数据库和对象之间关系 关 ...

  2. Django 查漏补缺

    Django 查漏补缺 Django  内容回顾: 一. Http 请求本质: 网络传输,运用socket Django程序: socket 服务端 a. 服务端监听IP和端口 b. 浏览器发送请求 ...

  3. Flutter查漏补缺1

    Flutter 基础知识查漏补缺 Hot reload原理 热重载分为这几个步骤 扫描项目改动:检查是否有新增,删除或者改动,直到找到上次编译后发生改变的dart代码 增量编译:找到改变的dart代码 ...

  4. 《CSS权威指南》基础复习+查漏补缺

    前几天被朋友问到几个CSS问题,讲道理么,接触CSS是从大一开始的,也算有3年半了,总是觉得自己对css算是熟悉的了.然而还是被几个问题弄的"一脸懵逼"... 然后又是刚入职新公司 ...

  5. js基础查漏补缺(更新)

    js基础查漏补缺: 1. NaN != NaN: 复制数组可以用slice: 数组的sort.reverse等方法都会改变自身: Map是一组键值对的结构,Set是key的集合: Array.Map. ...

  6. 2019Java查漏补缺(一)

    看到一个总结的知识: 感觉很全面的知识梳理,自己在github上总结了计算机网络笔记就很累了,猜想思维导图的方式一定花费了作者很大的精力,特共享出来.原文:java基础思维导图 自己学习的查漏补缺如下 ...

  7. 20165223 week1测试查漏补缺

    week1查漏补缺 经过第一周的学习后,在蓝墨云班课上做了一套31道题的小测试,下面是对测试题中遇到的错误的分析和总结: 一.背记题 不属于Java后继技术的是? Ptyhon Java后继技术有? ...

  8. 今天開始慢下脚步,開始ios技术知识的查漏补缺。

    从2014.6.30 開始工作算起. 如今已经是第416天了.不止不觉.时间过的真快. 通过对之前工作的总结.发现,你的知识面.会决定你面对问题时的态度.过程和结果. 简单来讲.知识面拓展了,你才干有 ...

  9. Mysql查漏补缺笔记

    目录 查漏补缺笔记2019/05/19 文件格式后缀 丢失修改,脏读,不可重复读 超键,候选键,主键 构S(Stmcture)/完整性I(Integrity)/数据操纵M(Malippulation) ...

随机推荐

  1. win_tc使用感受

    上大学的时候一直在使用win_tc就因为使用方便,今天准备用这个工具编辑一个函数,就特意下载了一个. 没想到直接出来一个bug. sizeof(char*)结果竟然是2. 果断接卸,误人子弟啊.

  2. jQuery筛选

    1.filter筛选出与指定表达式匹配的元素集合 html: <p>Hello</p><p>Hello Again</p><p class=&qu ...

  3. 洛谷——P1478 陶陶摘苹果(升级版)

    题目描述 又是一年秋季时,陶陶家的苹果树结了n个果子.陶陶又跑去摘苹果,这次她有一个a公分的椅子.当他手够不着时,他会站到椅子上再试试. 这次与NOIp2005普及组第一题不同的是:陶陶之前搬凳子,力 ...

  4. NGUI 简单的背包系统

    1.首先在场景中创建格子,用来存放物体的 2.为每一个格子设置标签为Item,建议只做一个格子,然后创建预制体就可以了,然后为每一个格子附加Box Collider组件,要用于检测嘛, 3.接下来就是 ...

  5. Maven环境配置及idea建Maven工程

    https://blog.csdn.net/qq_37497322/article/details/78988378

  6. 【bzoj2190】[SDOI2008]仪仗队 数论 欧拉函数 筛法

    http://www.lydsy.com/JudgeOnline/problem.php?id=2190   裸欧拉函数,先不计算对角线(a,a)的一列,然后算出1到n-1的所有欧拉函数相加*2,再加 ...

  7. 2017 icpc 沈阳网络赛

    cable cable cable Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  8. 【推导】【DFS】Codeforces Round #429 (Div. 1) B. Leha and another game about graph

    题意:给你一张图,给你每个点的权值,要么是-1,要么是1,要么是0.如果是-1就不用管,否则就要删除图中的某些边,使得该点的度数 mod 2等于该点的权值.让你输出一个留边的方案. 首先如果图内有-1 ...

  9. Java NIO:Buffer、Channel 和 Selector

    Buffer 一个 Buffer 本质上是内存中的一块,我们可以将数据写入这块内存,之后从这块内存获取数据. java.nio 定义了以下几个 Buffer 的实现,这个图读者应该也在不少地方见过了吧 ...

  10. Html 事件列表

    Html 事件列表 一般事件:onClick HTML: 鼠标点击事件,多用在某个对象控制的范围内的鼠标点击onDblClick HTML: 鼠标双击事件onMouseDown HTML: 鼠标上的按 ...