SSH框架之Struts(4)——Struts查漏补缺BeanUtils在Struts1中
在上篇博客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中的更多相关文章
- Entity Framework 查漏补缺 (一)
明确EF建立的数据库和对象之间的关系 EF也是一种ORM技术框架, 将对象模型和关系型数据库的数据结构对应起来,开发人员不在利用sql去操作数据相关结构和数据.以下是EF建立的数据库和对象之间关系 关 ...
- Django 查漏补缺
Django 查漏补缺 Django 内容回顾: 一. Http 请求本质: 网络传输,运用socket Django程序: socket 服务端 a. 服务端监听IP和端口 b. 浏览器发送请求 ...
- Flutter查漏补缺1
Flutter 基础知识查漏补缺 Hot reload原理 热重载分为这几个步骤 扫描项目改动:检查是否有新增,删除或者改动,直到找到上次编译后发生改变的dart代码 增量编译:找到改变的dart代码 ...
- 《CSS权威指南》基础复习+查漏补缺
前几天被朋友问到几个CSS问题,讲道理么,接触CSS是从大一开始的,也算有3年半了,总是觉得自己对css算是熟悉的了.然而还是被几个问题弄的"一脸懵逼"... 然后又是刚入职新公司 ...
- js基础查漏补缺(更新)
js基础查漏补缺: 1. NaN != NaN: 复制数组可以用slice: 数组的sort.reverse等方法都会改变自身: Map是一组键值对的结构,Set是key的集合: Array.Map. ...
- 2019Java查漏补缺(一)
看到一个总结的知识: 感觉很全面的知识梳理,自己在github上总结了计算机网络笔记就很累了,猜想思维导图的方式一定花费了作者很大的精力,特共享出来.原文:java基础思维导图 自己学习的查漏补缺如下 ...
- 20165223 week1测试查漏补缺
week1查漏补缺 经过第一周的学习后,在蓝墨云班课上做了一套31道题的小测试,下面是对测试题中遇到的错误的分析和总结: 一.背记题 不属于Java后继技术的是? Ptyhon Java后继技术有? ...
- 今天開始慢下脚步,開始ios技术知识的查漏补缺。
从2014.6.30 開始工作算起. 如今已经是第416天了.不止不觉.时间过的真快. 通过对之前工作的总结.发现,你的知识面.会决定你面对问题时的态度.过程和结果. 简单来讲.知识面拓展了,你才干有 ...
- Mysql查漏补缺笔记
目录 查漏补缺笔记2019/05/19 文件格式后缀 丢失修改,脏读,不可重复读 超键,候选键,主键 构S(Stmcture)/完整性I(Integrity)/数据操纵M(Malippulation) ...
随机推荐
- php定位并且获取天气信息
/** *获取天气预报信息 **/ header("Content-type: text/html; charset=utf-8"); class getWeather{ priv ...
- JS中事件绑定问题
今天编写代码时遇到一个问题,我的判断语句(IFLESE)老是顺序执行结束后又跳到中间的语句里去执行了,找了半天没发现问题,最后才发现是事件绑定闹得鬼,不多说,先上代码为敬. JSP里 <butt ...
- 子树(LintCode)
子树 有两个不同大小的二进制树: T1 有上百万的节点:T2 有好几百的节点.请设计一种算法,判定 T2 是否为 T1的子树. 样例 下面的例子中 T2 是 T1 的子树: 1 3 / \ / T1 ...
- Nginx反代,后端一个IP绑定多个SSL证书,导致连接失败之解决方法:HTTPS和SNI扩展
默认:SSL协议进行握手协商进行连接的时候,默认是不会发送主机名的,也就是是以IP的形式来进行https连接握手协商的,这就导致一个问题,当一台服务器上有多个虚拟主机使用同一个IP的时候, Nginx ...
- Sqli-labs介绍、下载、安装
SQLI和sqli-labs介绍 SQLI,sql injection,我们称之为sql注入.何为sql,英文:Structured Query Language,叫做结构化查询语言.常见的结构化数据 ...
- 【推导】AtCoder Regular Contest 082 D - Derangement
题意:给你一个排列a,每次可以交换相邻的两个数.让你用最少的交换次数使得a[i] != i. 对于两个相邻的a[i]==i的数,那么一次交换必然可以使得它们的a[i]都不等于i. 对于两个相邻的,其中 ...
- hibernate双向ManyToMany映射
工作需要一个双向多对多映射,照着李刚的书做了映射,碰到了一些问题,现就问题及解决方案进行总结归纳. 1.首先奉上最初代码 Person5.java @Entity @Table(name = &quo ...
- nodesj中 中间件express-session的理解
1.为什么使用session? session运行在服务器端,当客户端第一次访问服务器时,可以将客户的登录信息保存. 当客户访问其他页面时,可以判断客户的登录状态,做出提示,相当于登录拦截. sess ...
- KEIL、uVision、RealView、MDK、KEIL C51之间比较
KEIL uVision,KEIL MDK,KEIL For ARM,RealView MDK,KEIL C51,KEIL C166,KEIL C251 从接触MCS-51单片机开始,我们就知道有一个 ...
- OpenShift应用镜像构建(1) S2I tomcat 镜像定制
参考并感谢https://www.jianshu.com/p/fd3e62263046 在对接项目制作应用镜像的过程中,经常发现避免不了的是需要写Dockerfile,(当然另外一种方式是直接run一 ...