关闭

config包之FormBeanConfig浅析

627人阅读 评论(0) 收藏 举报

 org.apache.struts.config.FormBeanConfig解析

struts-config.xml文件中每一个<form-bean>都对应一个org.apache.struts.action.ActionForm Bean对象。该对象的实体类继承自org.apache.struts.config.FormBeanConfig类
<form-beans>元素用来配置多个ActionForm Bean。<form-beans>元素包含零个或多个<form-bean>子元素。如果配置动态ActionForm Bean还必须配置<form-bean>元素的<form-property>子元素
<form-bean>元素中的className是指定和<form-bean>元素对应的配置类,默认值为org.apache.struts.config.formBeanConfig,如果在这里设置自定义的类,该类必须扩展FormBeanConfig类
<form-bean>元素中的name是指定该ActionForm Bean的唯一标识符,整个Struts框架用该标识符来引用这个bean,该属性是必须的,它在FormBeanConfig类中对应的属性为String name
<form-bean>元素中的type是指定ActionForm类的完整类名,该属性是必须的,它在FormBeanConfig类中对应的属性是String type
<form-bean>元素中的dynamic是指定该ActionForm验证是动态验证,她在FormConfig类中对应的属性是boolean dynamic
以上四个对象在类中都有对应的get和set方法

FormBeanConfig中的HashMap formProperties属性是用来存储FormPropertyConfig对象的
FormBeanConfig中的DynaActionFormClass dynaActionFormClass是一个动态验证类的实例
FormBeanConfig中的boolen restricted属性是判断form是否为受限的,true为受限的,受限就是所有的FormPropertyConfig对象的类型和默认值已经加载完成

类中个方法解析
public class FormBeanConfig implements Serializable {

    protected boolean configured = false;

    protected HashMap formProperties = new HashMap();

    protected String lock = "";

    protected transient DynaActionFormClass dynaActionFormClass;

    public DynaActionFormClass getDynaActionFormClass() {

        if (dynamic == false) {
            throw new IllegalArgumentException("ActionForm is not dynamic");
        }
        synchronized (lock) {
            if (dynaActionFormClass == null) {
                dynaActionFormClass = new DynaActionFormClass(this);
            }
        }
        return dynaActionFormClass;
    }

    protected boolean dynamic = false;

    public boolean getDynamic() {
        return (this.dynamic);
    }

    public void setDynamic(boolean dynamic) {
        if (configured) {
            throw new IllegalStateException("Configuration is frozen");
        }
        ; // No action required
    }

    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 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;
        Class dynaBeanClass = DynaActionForm.class;
        Class formBeanClass = formBeanClass();
        if (formBeanClass != null) {
            if (dynaBeanClass.isAssignableFrom(formBeanClass)) {
                this.dynamic = true;
            } else {
                this.dynamic = false;
            }
        } else {
            this.dynamic = false;
        }
    }

    protected boolean restricted = false;

    public boolean isRestricted() {
        return restricted;
    }

    public void setRestricted(boolean restricted) {
        this.restricted = restricted;
    }

    //创建验证类实例
    public ActionForm createActionForm(ActionServlet servlet)
        throws IllegalAccessException, InstantiationException {

        Object obj = null;

        if (getDynamic()) {//判断该验证是否为动态验证,是返回true
            obj = getDynaActionFormClass().newInstance();//得到一个动态验证类DynaActionFormClass的实例
        } else {
            obj = formBeanClass().newInstance();//得到一个非动态验证类ActionForm的实例
        }

        ActionForm form = null;
        if (obj instanceof ActionForm) {//测试obj是否为ActionForm的实例
            form = (ActionForm)obj;//如果是就直接强类型转换为ActionForm
        } else  {
            form = new BeanValidatorForm(obj);如果不是就new出一个BeanvalidatorForm实例,返回一个动态验证类
     //BeanValidatorForm是Struts的一个标准的ValidatorForm类
        }

        form.setServlet(servlet);

        //将<form-property>对应的对象FormPropertyConfig对象中的 类型(type) 和 默认值(initial) 加载到form中
        if (form instanceof DynaBean && //以下代码不知该如何注释望高手添加
            ((DynaBean)form).getDynaClass() instanceof MutableDynaClass) {//判断是否为动态的验证
            DynaBean         dynaBean  = (DynaBean)form;
            MutableDynaClass dynaClass = (MutableDynaClass)dynaBean.getDynaClass();

            // Add properties
            dynaClass.setRestricted(false);
            FormPropertyConfig props[] = findFormPropertyConfigs();
            for (int i = 0; i < props.length; i++) {
                dynaClass.add(props[i].getName(), props[i].getTypeClass());
                dynaBean.set(props[i].getName(), props[i].initial());
            }
            dynaClass.setRestricted(isRestricted());
        }

        return form;

    }

    //将一个FormPropertyConfig对象放入Hash formProperties中
    public void addFormPropertyConfig(FormPropertyConfig config) {
        if (configured) {
            throw new IllegalStateException("Configuration is frozen");
        }
        if (formProperties.containsKey(config.getName())) {//如果此映射包含对于指定的键的映射关系,返回 true为包含
            throw new IllegalArgumentException("Property " +
                                               config.getName() +
                                               " already defined");
        }
        formProperties.put(config.getName(), config);
    }

    //通过Key把HashMap formProperties中的值取出
    public FormPropertyConfig findFormPropertyConfig(String name) {
        return ((FormPropertyConfig) formProperties.get(name));
    }

    //把HashMap formProperties中的值取出,并把它们转换成FormPropertyConfig[]数组
    public FormPropertyConfig[] findFormPropertyConfigs() {
        FormPropertyConfig results[] =
            new FormPropertyConfig[formProperties.size()];
        return ((FormPropertyConfig[]) formProperties.values().toArray(results));
    }

    public void freeze() {

        configured = true;

        FormPropertyConfig[] fpconfigs = findFormPropertyConfigs();
        for (int i = 0; i < fpconfigs.length; i++) {
            fpconfigs[i].freeze();
        }

    }

    //该方法删除FormPropertyConfig对象,该对象和<form-property>元素是相对应的
    public void removeFormPropertyConfig(FormPropertyConfig config) {
        if (configured) {
            throw new IllegalStateException("Configuration is frozen");
        }
        formProperties.remove(config.getName());
    }

    public String toString() {

        StringBuffer sb = new StringBuffer("FormBeanConfig[");
        sb.append("name=");
        sb.append(this.name);
        sb.append(",type=");
        sb.append(this.type);
        sb.append("]");
        return (sb.toString());

    }

    //加载<form-bean>元素中type的类,与FormPropertyConfig中加载类型类一样
    protected Class formBeanClass() {

        ClassLoader classLoader =
            Thread.currentThread().getContextClassLoader();
        if (classLoader == null) {
            classLoader = this.getClass().getClassLoader();
        }
        try {
            return (classLoader.loadClass(getType()));
        } catch (Exception e) {
            return (null);
        }

    }
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:8591次
    • 积分:171
    • 等级:
    • 排名:千里之外
    • 原创:8篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章存档