上一篇博客主要是讲解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方法:
进入这个方法,看到这个方法的实现源代码:
protectedvoid processPopulate(HttpServletRequest request,
HttpServletResponse response,
ActionForm form,
ActionMapping mapping)
throws ServletException {
if (form == null) {
return;
}
// Populate the bean properties of this ActionForm instance
if (log.isDebugEnabled()) {
log.debug(" Populating bean properties from this request");
}
form.setServlet(this.servlet);
form.reset(mapping, request);
if (mapping.getMultipartClass() != null) {
request.setAttribute(Globals.MULTIPART_KEY,
mapping.getMultipartClass());
}
RequestUtils.populate(form, mapping.getPrefix(), mapping.getSuffix(),
request);
// Set the cancellation request attribute if appropriate
if ((request.getParameter(Constants.CANCEL_PROPERTY) != null) ||
(request.getParameter(Constants.CANCEL_PROPERTY_X) != null)) {
request.setAttribute(Globals.CANCEL_KEY, Boolean.TRUE);
}
}
其中,form.reset(mapping, request);这个方法就是讲form重置,作用是使ActionForm中的值恢复初始状态。
下面RequestUtils.populate(form, mapping.getPrefix(), mapping.getSuffix()这个方法就是要完成填充map和转换类型等操作的,具体实现:
publicstaticvoid 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 {
thrownew 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) {
thrownew 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();
}
这段代码,这段代码主要是获得表单的所有名称,之后通过下面这段代码:
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值,之后通过
if (!(stripped.startsWith("org.apache.struts."))) {
properties.put(stripped, parameterValue);
}
将名称作为key值,讲名称的value值作为value值添加到map中,到此为止,我们就讲表单数据添加到了map中。
随后,调用第三方的组件来实现类型转换:
try {
BeanUtils.populate(bean, properties);
} catch(Exception e) {
thrownew 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);
}
}
这个方法会遍历ActionForm的值的类型,并且讲Map中的值的类型改为和ActionForm对应的类型。
到这里processPopulate的方法就实现完毕,实现了这个方法有什么用处呢?敬请等待下一篇博文!
分享到:
相关推荐
Struts_学习笔记之ActionForm,简单配置即可达到效果
最近自学java中的框架-struts写了一些小例子,这都是很经典的程序,如果大家瞧得起要下载去看看,顺便给俺找找不足的地方。我的qq 821865130 email qingtian_hechen@163.com 希望大家能多多给我帮助。在此谢谢各位!...
struts 视频 struts视频 actionform
最近自学java中的框架-struts写了一些小例子,这都是很经典的程序,如果大家瞧得起要下载去看看,顺便给俺找找不足的地方。我的qq 821865130 email qingtian_hechen@163.com 希望大家能多多给我帮助。在此谢谢各位!...
struts1使用actionform实现表单验证,表单验证失败追加信息。
NULL 博文链接:https://arang.iteye.com/blog/420546
struts框架详细介绍 struts框架的组成部分 struts框架的优势 ActionServlet的基本功能 ActionForm的使用
Struts Hibernate Spring 集成开发宝典 actionform 有研究或探讨或开源的请加群:37424970 或联系本人MSN或邮箱:zhuseahui@yahoo.com.cn
1 Struts框架 3 1.1 Struts压缩包内容 3 1.2 Struts体系结构 4 1.3 Struts框架中的组件 5 1.3.1 Struts配置文件 5 1.3.2 ActionServlet类 8 1.3.3 ActionMapping类 10 1.3.4 Action类 10 1.3.5 ActionForm类 11 ...
主要是actionForm的基本验证,重写validate方法,返回actionErrors,然后根据资源文件显示错误。 这是一个demo例子,直接导入eclispe中即可。 并且这部分内容会在相应的博客中介绍,稍后会更新博客。
该avi很好的讲解动态actionForm,通过练习可以理解它的运行过程
struts提供了标签来显示整个ActionErrors的错误信息。但要在struts-config.xml中设置<action ...
总体思路:这个验证是没有添加验证框架的验证,而是直接通过ActionForm的validate()方法进行系统验证, 进行验证要处理三个方面的问题: > 配置资源文件:配置ApplicationResources.prop内容,把验证的的内容写道...
把JSP放到WEB-INF后以保护JSP源代码 22 使用 Prebuilt Action类提升开发效率 23 Struts标记库 25 定制JSP标记 25 资源束 26 Bean标记 27 Bean复制标记 27 定义脚本变量的标记 28 显示Bean属性 29 消息标记...
Struts框架中提供一系列的自定义标记,用来在视图层实现数据的表示。...例如,在Struts框架中有一个标记,这个标记是用来提交表单的,Struts框架够把这个表单的数据自动映射到相对应的ActionForm Bean中去
把JSP放到WEB-INF后以保护JSP源代码 22 使用 Prebuilt Action类提升开发效率 23 Struts标记库 25 定制JSP标记 25 资源束 26 Bean标记 27 Bean复制标记 27 定义脚本变量的标记 28 显示Bean属性 29 消息标记和国际化 ...
代码完整,使用struta2做的自动表单提交功能。具有良好的后期维护性
比上面的完整 博文链接:https://shangdiyefankun.iteye.com/blog/122443
在本系列教程中我们将学习到 Struts2 的各种技术。在本教程中使用的工具和程序库的版本 如下: 开发工具: MyEclipse6 Web 服务器: Tomcat6 Struts 版本: Struts2.0.11.1 JDK 版本: JDK1.5.0_12 J2EE 版本: Java...