请教actionservlet填充actionform的实现原理 没看过源代码。你可以去看看他源代码(估计和下面一样)。你可以用apache的commons类库BeanUtils来完成这个自动填充的功能 :) 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 谢谢回复,能列一下BeanUtils的实现代码吗 // Decompiled by Jad v1.5.7g. Copyright 2000 Pavel Kouznetsov.// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html// Decompiler options: packimports(3) fieldsfirst ansi // Source File Name: BeanUtils.javapackage org.apache.commons.beanutils;import java.beans.IndexedPropertyDescriptor;import java.beans.PropertyDescriptor;import java.lang.reflect.Array;import java.lang.reflect.InvocationTargetException;import java.util.*;import org.apache.commons.collections.FastHashMap;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;// Referenced classes of package org.apache.commons.beanutils:// DynaBean, MappedPropertyDescriptor, PropertyUtils, DynaClass, // DynaProperty, ConvertUtils, Converterpublic class BeanUtils{ private static FastHashMap dummy = new FastHashMap(); private static Log log; private static int debug = 0; static Class class$org$apache$commons$beanutils$BeanUtils; /* synthetic field */ public BeanUtils() { } public static int getDebug() { return debug; } public static void setDebug(int newDebug) { debug = newDebug; } public static Object cloneBean(Object bean) throws IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException { if(log.isDebugEnabled()) log.debug("Cloning bean: " + bean.getClass().getName()); Class clazz = bean.getClass(); Object newBean = clazz.newInstance(); PropertyUtils.copyProperties(newBean, bean); return newBean; } public static void copyProperties(Object dest, Object orig) throws IllegalAccessException, InvocationTargetException { if(dest == null) throw new IllegalArgumentException("No destination bean specified"); if(orig == null) throw new IllegalArgumentException("No origin bean specified"); if(log.isDebugEnabled()) log.debug("BeanUtils.copyProperties(" + dest + ", " + orig + ")"); if(orig instanceof DynaBean) { DynaProperty origDescriptors[] = ((DynaBean)orig).getDynaClass().getDynaProperties(); for(int i = 0; i < origDescriptors.length; i++) { String name = origDescriptors[i].getName(); if(PropertyUtils.isWriteable(dest, name)) { Object value = ((DynaBean)orig).get(name); copyProperty(dest, name, value); } } } else if(orig instanceof Map) { for(Iterator names = ((Map)orig).keySet().iterator(); names.hasNext();) { String name = (String)names.next(); if(PropertyUtils.isWriteable(dest, name)) { Object value = ((Map)orig).get(name); copyProperty(dest, name, value); } } } else { PropertyDescriptor origDescriptors[] = PropertyUtils.getPropertyDescriptors(orig); for(int i = 0; i < origDescriptors.length; i++) { String name = origDescriptors[i].getName(); if(!"class".equals(name) && PropertyUtils.isReadable(orig, name) && PropertyUtils.isWriteable(dest, name)) try { Object value = PropertyUtils.getSimpleProperty(orig, name); copyProperty(dest, name, value); } catch(NoSuchMethodException e) { } } } } public static void copyProperty(Object bean, String name, Object value) throws IllegalAccessException, InvocationTargetException { if(log.isTraceEnabled()) { StringBuffer sb = new StringBuffer(" copyProperty("); sb.append(bean); sb.append(", "); sb.append(name); sb.append(", "); if(value == null) sb.append("<NULL>"); else if(value instanceof String) sb.append((String)value); else if(value instanceof String[]) { String values[] = (String[])value; sb.append('['); for(int i = 0; i < values.length; i++) { if(i > 0) sb.append(','); sb.append(values[i]); } sb.append(']'); } else { sb.append(value.toString()); } sb.append(')'); log.trace(sb.toString()); } Object target = bean; int delim = name.lastIndexOf(46); if(delim >= 0) { try { target = PropertyUtils.getProperty(bean, name.substring(0, delim)); } catch(NoSuchMethodException e) { return; } name = name.substring(delim + 1); if(log.isTraceEnabled()) { log.trace(" Target bean = " + target); log.trace(" Target name = " + name); } } String propName = null; Class type = null; int index = -1; String key = null; propName = name; int i = propName.indexOf(91); if(i >= 0) { int k = propName.indexOf(93); try { index = Integer.parseInt(propName.substring(i + 1, k)); } catch(NumberFormatException e) { } propName = propName.substring(0, i); } int j = propName.indexOf(40); if(j >= 0) { int k = propName.indexOf(41); try { key = propName.substring(j + 1, k); } catch(IndexOutOfBoundsException e) { } propName = propName.substring(0, j); } if(target instanceof DynaBean) { DynaClass dynaClass = ((DynaBean)target).getDynaClass(); DynaProperty dynaProperty = dynaClass.getDynaProperty(propName); if(dynaProperty == null) return; type = dynaProperty.getType(); } else { PropertyDescriptor descriptor = null; try { descriptor = PropertyUtils.getPropertyDescriptor(target, name); if(descriptor == null) return; } catch(NoSuchMethodException e) { return; } type = descriptor.getPropertyType(); if(type == null) { if(log.isTraceEnabled()) log.trace(" target type for property '" + propName + "' is null, so skipping ths setter"); return; } } if(log.isTraceEnabled()) log.trace(" target propName=" + propName + ", type=" + type + ", index=" + index + ", key=" + key); if(index >= 0) { Converter converter = ConvertUtils.lookup(type.getComponentType()); if(converter != null) { log.trace(" USING CONVERTER " + converter); value = converter.convert(type, value); } try { PropertyUtils.setIndexedProperty(target, propName, index, value); } catch(NoSuchMethodException e) { throw new InvocationTargetException(e, "Cannot set " + propName); } } else if(key != null) { try { PropertyUtils.setMappedProperty(target, propName, key, value); } catch(NoSuchMethodException e) { throw new InvocationTargetException(e, "Cannot set " + propName); } } else { Converter converter = ConvertUtils.lookup(type); if(converter != null) { log.trace(" USING CONVERTER " + converter); value = converter.convert(type, value); } try { PropertyUtils.setSimpleProperty(target, propName, value); } catch(NoSuchMethodException e) { throw new InvocationTargetException(e, "Cannot set " + propName); } } } public static Map describe(Object bean) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { if(bean == null) return new HashMap(); if(log.isDebugEnabled()) log.debug("Describing bean: " + bean.getClass().getName()); Map description = new HashMap(); if(bean instanceof DynaBean) { DynaProperty descriptors[] = ((DynaBean)bean).getDynaClass().getDynaProperties(); for(int i = 0; i < descriptors.length; i++) { String name = descriptors[i].getName(); description.put(name, getProperty(bean, name)); } } else { PropertyDescriptor descriptors[] = PropertyUtils.getPropertyDescriptors(bean); for(int i = 0; i < descriptors.length; i++) { String name = descriptors[i].getName(); if(descriptors[i].getReadMethod() != null) description.put(name, getProperty(bean, name)); } } return description; } public static String[] getArrayProperty(Object bean, String name) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { Object value = PropertyUtils.getProperty(bean, name); if(value == null) return null; if(value instanceof Collection) { ArrayList values = new ArrayList(); for(Iterator items = ((Collection)value).iterator(); items.hasNext();) { Object item = items.next(); if(item == null) values.add((String)null); else values.add(item.toString()); } return (String[])values.toArray(new String[values.size()]); } if(value.getClass().isArray()) { int n = Array.getLength(value); String results[] = new String[n]; for(int i = 0; i < n; i++) { Object item = Array.get(value, i); if(item == null) results[i] = null; else results[i] = item.toString(); } return results; } else { String results[] = new String[1]; results[0] = value.toString(); return results; } } public static String getIndexedProperty(Object bean, String name) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { Object value = PropertyUtils.getIndexedProperty(bean, name); return ConvertUtils.convert(value); } public static String getIndexedProperty(Object bean, String name, int index) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { Object value = PropertyUtils.getIndexedProperty(bean, name, index); return ConvertUtils.convert(value); } public static String getMappedProperty(Object bean, String name) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { Object value = PropertyUtils.getMappedProperty(bean, name); return ConvertUtils.convert(value); } public static String getMappedProperty(Object bean, String name, String key) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { Object value = PropertyUtils.getMappedProperty(bean, name, key); return ConvertUtils.convert(value); } public static String getNestedProperty(Object bean, String name) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { Object value = PropertyUtils.getNestedProperty(bean, name); return ConvertUtils.convert(value); } public static String getProperty(Object bean, String name) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { return getNestedProperty(bean, name); } public static String getSimpleProperty(Object bean, String name) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { Object value = PropertyUtils.getSimpleProperty(bean, name); return ConvertUtils.convert(value); } public static void populate(Object bean, Map properties) throws IllegalAccessException, InvocationTargetException { if(bean == null || properties == null) return; if(log.isDebugEnabled()) log.debug("BeanUtils.populate(" + bean + ", " + properties + ")"); for(Iterator names = properties.keySet().iterator(); names.hasNext();) { String name = (String)names.next(); if(name != null) { Object value = properties.get(name); setProperty(bean, name, value); } } } 是actionservlet的源代码吗?在什么目录能看到?能大概说一下程序过程吗?谢谢 是通过BeanUtils类实现.具体可以看一下<<struts in action>> 直接下载 struts 的源代码看!ActionServlet.java 呵呵,就是不想直接去看那个东东,想听听各位高手消化STRUTS源码后的看法 jsp提交表单时,action不执行set方法,导致action取不到页面传来的值 怎么去掉项目加载spring之后出现的一堆这样的log? 求一个CAS单点登陆的例子 jsp注册新用户问题 联合主键的外键问题 struts2上传文件时拦截器不起作用 ssh整合报错,大家帮助看下。 Sturts学习经典视频 Java爱好者启动RoR十分钟体验计划,入门者Step By Step体验一 jfreechart如何在一个图中实现柱状图与曲线图的混合 [求助] Log4j按类输出日志 帮忙介绍一本比较好的开发jsp自定义标签的书
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi
// Source File Name: BeanUtils.javapackage org.apache.commons.beanutils;import java.beans.IndexedPropertyDescriptor;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import org.apache.commons.collections.FastHashMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;// Referenced classes of package org.apache.commons.beanutils:
// DynaBean, MappedPropertyDescriptor, PropertyUtils, DynaClass,
// DynaProperty, ConvertUtils, Converterpublic class BeanUtils
{ private static FastHashMap dummy = new FastHashMap();
private static Log log;
private static int debug = 0;
static Class class$org$apache$commons$beanutils$BeanUtils; /* synthetic field */ public BeanUtils()
{
} public static int getDebug()
{
return debug;
} public static void setDebug(int newDebug)
{
debug = newDebug;
} public static Object cloneBean(Object bean)
throws IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException
{
if(log.isDebugEnabled())
log.debug("Cloning bean: " + bean.getClass().getName());
Class clazz = bean.getClass();
Object newBean = clazz.newInstance();
PropertyUtils.copyProperties(newBean, bean);
return newBean;
} public static void copyProperties(Object dest, Object orig)
throws IllegalAccessException, InvocationTargetException
{
if(dest == null)
throw new IllegalArgumentException("No destination bean specified");
if(orig == null)
throw new IllegalArgumentException("No origin bean specified");
if(log.isDebugEnabled())
log.debug("BeanUtils.copyProperties(" + dest + ", " + orig + ")");
if(orig instanceof DynaBean)
{
DynaProperty origDescriptors[] = ((DynaBean)orig).getDynaClass().getDynaProperties();
for(int i = 0; i < origDescriptors.length; i++)
{
String name = origDescriptors[i].getName();
if(PropertyUtils.isWriteable(dest, name))
{
Object value = ((DynaBean)orig).get(name);
copyProperty(dest, name, value);
}
} } else
if(orig instanceof Map)
{
for(Iterator names = ((Map)orig).keySet().iterator(); names.hasNext();)
{
String name = (String)names.next();
if(PropertyUtils.isWriteable(dest, name))
{
Object value = ((Map)orig).get(name);
copyProperty(dest, name, value);
}
} } else
{
PropertyDescriptor origDescriptors[] = PropertyUtils.getPropertyDescriptors(orig);
for(int i = 0; i < origDescriptors.length; i++)
{
String name = origDescriptors[i].getName();
if(!"class".equals(name) && PropertyUtils.isReadable(orig, name) && PropertyUtils.isWriteable(dest, name))
try
{
Object value = PropertyUtils.getSimpleProperty(orig, name);
copyProperty(dest, name, value);
}
catch(NoSuchMethodException e) { }
} }
}
throws IllegalAccessException, InvocationTargetException
{
if(log.isTraceEnabled())
{
StringBuffer sb = new StringBuffer(" copyProperty(");
sb.append(bean);
sb.append(", ");
sb.append(name);
sb.append(", ");
if(value == null)
sb.append("<NULL>");
else
if(value instanceof String)
sb.append((String)value);
else
if(value instanceof String[])
{
String values[] = (String[])value;
sb.append('[');
for(int i = 0; i < values.length; i++)
{
if(i > 0)
sb.append(',');
sb.append(values[i]);
} sb.append(']');
} else
{
sb.append(value.toString());
}
sb.append(')');
log.trace(sb.toString());
}
Object target = bean;
int delim = name.lastIndexOf(46);
if(delim >= 0)
{
try
{
target = PropertyUtils.getProperty(bean, name.substring(0, delim));
}
catch(NoSuchMethodException e)
{
return;
}
name = name.substring(delim + 1);
if(log.isTraceEnabled())
{
log.trace(" Target bean = " + target);
log.trace(" Target name = " + name);
}
}
String propName = null;
Class type = null;
int index = -1;
String key = null;
propName = name;
int i = propName.indexOf(91);
if(i >= 0)
{
int k = propName.indexOf(93);
try
{
index = Integer.parseInt(propName.substring(i + 1, k));
}
catch(NumberFormatException e) { }
propName = propName.substring(0, i);
}
int j = propName.indexOf(40);
if(j >= 0)
{
int k = propName.indexOf(41);
try
{
key = propName.substring(j + 1, k);
}
catch(IndexOutOfBoundsException e) { }
propName = propName.substring(0, j);
}
if(target instanceof DynaBean)
{
DynaClass dynaClass = ((DynaBean)target).getDynaClass();
DynaProperty dynaProperty = dynaClass.getDynaProperty(propName);
if(dynaProperty == null)
return;
type = dynaProperty.getType();
} else
{
PropertyDescriptor descriptor = null;
try
{
descriptor = PropertyUtils.getPropertyDescriptor(target, name);
if(descriptor == null)
return;
}
catch(NoSuchMethodException e)
{
return;
}
type = descriptor.getPropertyType();
if(type == null)
{
if(log.isTraceEnabled())
log.trace(" target type for property '" + propName + "' is null, so skipping ths setter");
return;
}
}
if(log.isTraceEnabled())
log.trace(" target propName=" + propName + ", type=" + type + ", index=" + index + ", key=" + key);
if(index >= 0)
{
Converter converter = ConvertUtils.lookup(type.getComponentType());
if(converter != null)
{
log.trace(" USING CONVERTER " + converter);
value = converter.convert(type, value);
}
try
{
PropertyUtils.setIndexedProperty(target, propName, index, value);
}
catch(NoSuchMethodException e)
{
throw new InvocationTargetException(e, "Cannot set " + propName);
}
} else
if(key != null)
{
try
{
PropertyUtils.setMappedProperty(target, propName, key, value);
}
catch(NoSuchMethodException e)
{
throw new InvocationTargetException(e, "Cannot set " + propName);
}
} else
{
Converter converter = ConvertUtils.lookup(type);
if(converter != null)
{
log.trace(" USING CONVERTER " + converter);
value = converter.convert(type, value);
}
try
{
PropertyUtils.setSimpleProperty(target, propName, value);
}
catch(NoSuchMethodException e)
{
throw new InvocationTargetException(e, "Cannot set " + propName);
}
}
}
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
{
if(bean == null)
return new HashMap();
if(log.isDebugEnabled())
log.debug("Describing bean: " + bean.getClass().getName());
Map description = new HashMap();
if(bean instanceof DynaBean)
{
DynaProperty descriptors[] = ((DynaBean)bean).getDynaClass().getDynaProperties();
for(int i = 0; i < descriptors.length; i++)
{
String name = descriptors[i].getName();
description.put(name, getProperty(bean, name));
} } else
{
PropertyDescriptor descriptors[] = PropertyUtils.getPropertyDescriptors(bean);
for(int i = 0; i < descriptors.length; i++)
{
String name = descriptors[i].getName();
if(descriptors[i].getReadMethod() != null)
description.put(name, getProperty(bean, name));
} }
return description;
} public static String[] getArrayProperty(Object bean, String name)
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
{
Object value = PropertyUtils.getProperty(bean, name);
if(value == null)
return null;
if(value instanceof Collection)
{
ArrayList values = new ArrayList();
for(Iterator items = ((Collection)value).iterator(); items.hasNext();)
{
Object item = items.next();
if(item == null)
values.add((String)null);
else
values.add(item.toString());
} return (String[])values.toArray(new String[values.size()]);
}
if(value.getClass().isArray())
{
int n = Array.getLength(value);
String results[] = new String[n];
for(int i = 0; i < n; i++)
{
Object item = Array.get(value, i);
if(item == null)
results[i] = null;
else
results[i] = item.toString();
} return results;
} else
{
String results[] = new String[1];
results[0] = value.toString();
return results;
}
} public static String getIndexedProperty(Object bean, String name)
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
{
Object value = PropertyUtils.getIndexedProperty(bean, name);
return ConvertUtils.convert(value);
} public static String getIndexedProperty(Object bean, String name, int index)
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
{
Object value = PropertyUtils.getIndexedProperty(bean, name, index);
return ConvertUtils.convert(value);
} public static String getMappedProperty(Object bean, String name)
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
{
Object value = PropertyUtils.getMappedProperty(bean, name);
return ConvertUtils.convert(value);
} public static String getMappedProperty(Object bean, String name, String key)
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
{
Object value = PropertyUtils.getMappedProperty(bean, name, key);
return ConvertUtils.convert(value);
} public static String getNestedProperty(Object bean, String name)
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
{
Object value = PropertyUtils.getNestedProperty(bean, name);
return ConvertUtils.convert(value);
} public static String getProperty(Object bean, String name)
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
{
return getNestedProperty(bean, name);
} public static String getSimpleProperty(Object bean, String name)
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
{
Object value = PropertyUtils.getSimpleProperty(bean, name);
return ConvertUtils.convert(value);
} public static void populate(Object bean, Map properties)
throws IllegalAccessException, InvocationTargetException
{
if(bean == null || properties == null)
return;
if(log.isDebugEnabled())
log.debug("BeanUtils.populate(" + bean + ", " + properties + ")");
for(Iterator names = properties.keySet().iterator(); names.hasNext();)
{
String name = (String)names.next();
if(name != null)
{
Object value = properties.get(name);
setProperty(bean, name, value);
}
} }