org.apache.struts.config.FormPropertyConfig类解析
struts-config.xml文件中每一个<form-property>元素对应着一个org.apache.struts.config.FormPropertyConfig对象。FormPropertyConfig类与FormBeanConfig相关联
<form-property>元素中的className是指定和<form-property>元素对应的配置类,默认值为org.apache.struts.config.FormPropertyConfig。
<form-property>元素中的initial是以字符串的形式设置表但字段的初始值。如果没有设置该属性,则基本类型的表单字段的默认值为0,对象类型的表单字段默认值为null,该元素在FormPropertyConfig类对应的属性是String initial。
<form-property>元素中的name是指定表单字段的名字,该属性是必须的,它在FormPropertyConfig类中对应的属性是String name。
<form-property>元素中的type是指定表单字段的类型,如果表但字段为JAVA类,必须给出完整的类名,该属性是必须的,它在FormPropertyConfig类中对应的属性是String type。
<form-property>元素中的size是指定表单字段的输入长度,它在FormPropertyConfig类中对应的属性是int size。
类中方法解析
public class FormPropertyConfig implements Serializable {
private static final Log log =
LogFactory.getLog(FormPropertyConfig.class);
public FormPropertyConfig() {
super();
}
public FormPropertyConfig(String name, String type, String initial) {
this(name, type, initial, 0);
}
public FormPropertyConfig(String name, String type,
String initial, int size) {
super();
setName(name);
setType(type);
setInitial(initial);
setSize(size);
}
protected boolean configured = false;
protected String initial = null;
public String getInitial() {
return (this.initial);
}
public void setInitial(String initial) {
if (configured) {
throw new IllegalStateException("Configuration is frozen");
}
this.initial = initial;
}
protected String name = null;
public String getName() {
return (this.name);
}
public void setName(String name) {
if (configured) {
throw new IllegalStateException("Configuration is frozen");
}
this.name = name;
}
protected int size = 0;
public int getSize() {
return (this.size);
}
public void setSize(int size) {
if (configured) {
throw new IllegalStateException("Configuration is frozen");
}
if (size < 0) {
throw new IllegalArgumentException("size < 0");
}
this.size = size;
}
protected String type = null;
public String getType() {
return (this.type);
}
public void setType(String type) {
if (configured) {
throw new IllegalStateException("Configuration is frozen");
}
this.type = type;
}
//获取该表单字段的类型
public Class getTypeClass() {
String baseType = getType();//获取类中的type属性的值
boolean indexed = false;//判断type值所指定的的JAVA类型名,true则为数组类型
if (baseType.endsWith("[]")) {//判断属性的后缀是不是有“[]”,如果有则为数组类型
baseType = baseType.substring(0, baseType.length() - 2);//如果属性后缀有“[]”就截掉该后缀
indexed = true;
}
Class baseClass = null;
//判断type属性值是否为基础类型名
if ("boolean".equals(baseType)) {
baseClass = Boolean.TYPE;
} else if ("byte".equals(baseType)) {
baseClass = Byte.TYPE;
} else if ("char".equals(baseType)) {
baseClass = Character.TYPE;
} else if ("double".equals(baseType)) {
baseClass = Double.TYPE;
} else if ("float".equals(baseType)) {
baseClass = Float.TYPE;
} else if ("int".equals(baseType)) {
baseClass = Integer.TYPE;
} else if ("long".equals(baseType)) {
baseClass = Long.TYPE;
} else if ("short".equals(baseType)) {
baseClass = Short.TYPE;
} else {//type属性值是其他对象类型名
ClassLoader classLoader =//获取ClassLoader对象,类加载器
Thread.currentThread().getContextClassLoader();//Thread类中的静态方法currentThread()返回一个当前正在执行的线程对象,利用该线程对象调用getcontextClassLoader()返回该线程上下文的一个ClassLoader对象
if (classLoader == null) {//如果ClassLoader对象为空,则用当前对象构造一个加载器
classLoader = this.getClass().getClassLoader();
}
try {
baseClass = classLoader.loadClass(baseType);//加载一个类名为type属性值的类
} catch (Throwable t) {
baseClass = null;
}
}
// Return the base class or an array appropriately
if (indexed) {//如果type是一个数组类型名则创建一个数组的组件类型是baseClass对象,维度为0的数组它返回一个Object,然后得到一个对象运行时类
return (Array.newInstance(baseClass, 0).getClass());
} else {
return (baseClass);
}
}
public Object initial() {
Object initialValue = null;
try {
Class clazz = getTypeClass();
if (clazz.isArray()) {//判断对象是否为数组类型
if (initial != null) {
initialValue =//当他不为空的时候,调用ConvertUtils类的静态方法得到默认值对象
ConvertUtils.convert(initial, clazz);
} else {
initialValue =//如果为空,就构造一个数组组件类型为clazz.getComponentType(),维度为size的数组,返回一个Object
Array.newInstance(clazz.getComponentType(), size);
if (!(clazz.getComponentType().isPrimitive())) {//判断对象不是一个基本类型
for (int i = 0; i < size; i++) {
try {
Array.set(initialValue, i,//将指定数组对象中索引组件的值设置为指定的新值
clazz.getComponentType().newInstance());
} catch (Throwable t) {
log.error("Unable to create instance of " + clazz.getName() +
" for property=" + name+
", type=" + type +
", initial=" + initial +
", size=" + size + ".");
//FIXME: Should we just dump the entire application/module ?
}
}
}
}
} else {//对象不是数组
if (initial != null) {//对象不为空则调用ConvertUtils.convert方法获取值
initialValue = ConvertUtils.convert(initial, clazz);
} else {//为空则直接返回Object
initialValue = clazz.newInstance();
}
}
} catch (Throwable t) {
initialValue = null;
}
return (initialValue);
}
public void freeze() {
configured = true;
}
public String toString() {
StringBuffer sb = new StringBuffer("FormPropertyConfig[");
sb.append("name=");
sb.append(this.name);
sb.append(",type=");
sb.append(this.type);
sb.append(",initial=");
sb.append(this.initial);
sb.append("]");
return (sb.toString());
}
}
参阅资料:Struts底层源码,org.apache.struts.FormPropertyConfig类