Dorado 5 : 3.2.探讨ViewModel (T32)

ViewModel是Dataset和各种控件的容器,以及用于封装界面逻辑和操作逻辑的对象。
ViewModel在设计时一般存在一个view.xml和一个实现接口com.bstek.dorado.view.ViewModel的实现类。view.xml可以存在也可以不存在,但ViewModel的实现类是一定需要的。可能我们在设计ViewModel的时候一般只需要设置view.xml,而不会新建一个ViewModel实现类,其实我们每一份view.xml都会对应一个ViewModel实现类,在Setting里就有这样的一个必需属性<property name="view.defaultViewModel" value="com.bstek.dorado.view.impl.DynaViewModel"/>,如果在view.xml里view节点的clazz属性没有定义,那Dorado会把Setting定义好的view.defaultViewModel赋值给clazz属性。为了系统开发过程中保持系统的统一性和稳定性,我们一般对某个view.xml新建的ViewModel实现类,该实现类都会继承Setting定义好的view.defaultViewModel,除非你真正需要自己设计ViewModel的逻辑,那你也可以新建一个接口为com.bstek.dorado.view.ViewModel的实现类,这种方式是不建议的,问题也是不可控制的。根据上面所描述的内容,那我们在页面jsp定义view标签的时候一般设定config属性来定义该页面通过哪个view.xml来定义的,如果不设定config属性也可以通过设定clazz属性来定义该页面对应的ViewModel实现类。从Dorado实现的原理来说ViewModel实现类是必需的,而从一般开发人员来说可能就是view.xml是"必需"(经常使用)的。
通过下面一个HelloWorld例子不同的实现方式来说明View与ViewModel之间的关系:
我们来实现这样的一个页面:

view.xml+jsp(一般简单做法)

view.xml配置为:

<?xml version="1.0" encoding="UTF-8"?> 
 <view> 
 <Datasets> 
 <Dataset type="Wrapper" keyFields="EMPLOYEE_ID" id="datasetEmployee" originTable="EMPLOYEE" wrappedType="AutoSql" pageSize="10"> 
 <MasterLink/> 
 <Fields> 
 <Field label="员工编号" dataType="string" table="EMPLOYEE" originField="EMPLOYEE_ID" name="EMPLOYEE_ID"> 
 <Properties/> 
 </Field> 
 <Field label="部门" dataType="string" table="EMPLOYEE" originField="DEPT_ID" name="DEPT_ID"> 
 <Properties/> 
 </Field> 
 <Field label="员工姓名" dataType="string" table="EMPLOYEE" originField="EMPLOYEE_NAME" name="EMPLOYEE_NAME"> 
 <Properties/> 
 </Field> 
 <Field dropDown="dropdownSex" label="性别" dataType="" table="EMPLOYEE" originField="SEX" name="SEX"> 
 <Properties/> 
 </Field> 
 <Field label="出生日期" dataType="date" table="EMPLOYEE" originField="BIRTHDAY" name="BIRTHDAY"> 
 <Properties/> 
 </Field> 
 <Field dataType="" table="EMPLOYEE" originField="MARRIED" name="MARRIED"> 
 <Properties/> 
 </Field> 
 <Field dataType="double" table="EMPLOYEE" originField="SALARY" name="SALARY"> 
 <Properties/> 
 </Field> 
 <Field dataType="string" table="EMPLOYEE" originField="DEGREE" name="DEGREE"> 
 <Properties/> 
 </Field> 
 <Field dataType="string" table="EMPLOYEE" originField="EMAIL" name="EMAIL"> 
 <Properties/> 
 </Field> 
 <Field dataType="string" table="EMPLOYEE" originField="WEB" name="WEB"> 
 <Properties/> 
 </Field> 
 <Field dataType="string" table="EMPLOYEE" originField="CMNT" name="CMNT"> 
 <Properties/> 
 </Field> 
 </Fields> 
 <Joins/> 
 <MatchRules> 
 <MatchRule escapeEnabled="true" level="1" operator="=" dataType="string" value=":EMPLOYEE_ID" originField="EMPLOYEE_ID" table="EMPLOYEE"/> 
 <MatchRule escapeEnabled="true" level="1" operator="like" dataType="string" value=":EMPLOYEE_NAME" originField="EMPLOYEE_NAME" table="EMPLOYEE"/> 
 <MatchRule escapeEnabled="true" level="1" operator="=" dataType="string" value=":DEPT_ID" originField="DEPT_ID" table="EMPLOYEE"/> 
 <MatchRule escapeEnabled="true" level="1" operator="=" dataType="" value=":SEX" originField="SEX" table="EMPLOYEE"/> 
 </MatchRules> 
 <SortRules/> 
 <Parameters/> 
 <Properties/> 
 </Dataset> 
 <Dataset type="Form" autoLoadData="false" id="datasetForm" insertOnEmpty="true"> 
 <MasterLink/> 
 <Fields> 
 <Field name="EMPLOYEE_ID" label="员工编号" dataType="string"> 
 <Properties/> 
 </Field> 
 <Field name="DEPT_ID" dropDown="dropdownDept" label="部门" dataType="string"> 
 <Properties/> 
 </Field> 
 <Field name="EMPLOYEE_NAME" label="员工姓名" dataType="string"> 
 <Properties/> 
 </Field> 
 <Field name="SEX" dropDown="dropdownSex" label="性别" dataType=""> 
 <Properties/> 
 </Field> 
 </Fields> 
 <Parameters/> 
 <Properties/> 
 </Dataset> 
 <Dataset type="Wrapper" id="datasetDept" originTable="DEPT" wrappedType="AutoSql"> 
 <MasterLink/> 
 <Fields> 
 <Field dataType="string" table="DEPT" originField="DEPT_ID" name="DEPT_ID"> 
 <Properties/> 
 </Field> 
 <Field dataType="string" table="DEPT" originField="BRANCH_ID" name="BRANCH_ID"> 
 <Properties/> 
 </Field> 
 <Field dataType="string" table="DEPT" originField="DEPT_NAME" name="DEPT_NAME"> 
 <Properties/> 
 </Field> 
 </Fields> 
 <Joins/> 
 <MatchRules/> 
 <SortRules/> 
 <Parameters/> 
 <Properties/> 
 </Dataset> 
 </Datasets> 
 <Controls> 
 <Control type="DataTable" showHScrollBar="false" dataset="datasetEmployee" height="100%" id="tableEmployee" width="100%"> 
 <Column field="EMPLOYEE_ID" name="EMPLOYEE_ID"/> 
 <Column field="DEPT_ID" name="DEPT_ID"/> 
 <Column field="EMPLOYEE_NAME" name="EMPLOYEE_NAME"/> 
 <Column field="SEX" name="SEX"/> 
 <Column field="BIRTHDAY" name="BIRTHDAY"/> 
 </Control> 
 <Control type="AutoForm" dataset="datasetForm" id="formForm"> 
 <FormGroup title="员工查询"> 
 <Element name="EMPLOYEE_ID" type="TextEditor" field="EMPLOYEE_ID"> 
 <FieldLabel/> 
 <TextEditor/> 
 </Element> 
 <Element name="EMPLOYEE_NAME" type="TextEditor" field="EMPLOYEE_NAME"> 
 <FieldLabel/> 
 <TextEditor/> 
 </Element> 
 <Element name="DEPT_ID" type="TextEditor" field="DEPT_ID"> 
 <FieldLabel/> 
 <TextEditor/> 
 </Element> 
 <Element name="SEX" type="TextEditor" field="SEX"> 
 <FieldLabel/> 
 <TextEditor/> 
 </Element> 
 <Element name="buttonQuery" controlAlign="right" showLabel="false" colSpan="2" type="Custom" controlId="buttonQuery"> 
 <FieldLabel/> 
 </Element> 
 </FormGroup> 
 </Control> 
 <Control id="commandQuery" queryDataset="datasetEmployee" type="QueryCommand" conditionDataset="datasetForm"> 
 <Parameters/> 
 <Events> 
 <Event name="beforeExecute">var record = datasetForm.getCurrent();  
 if(record){  
 var EMPLOYEE_NAME = record.getValue("EMPLOYEE_NAME");  
 if(EMPLOYEE_NAME){  
 EMPLOYEE_NAME = "%" + EMPLOYEE_NAME + "%";  
 command.parameters().setValue("EMPLOYEE_NAME",EMPLOYEE_NAME);  
 }else{  
 command.parameters().removeParameter("EMPLOYEE_NAME");  
 }  
 }  
 </Event> 
 </Events> 
 </Control> 
 <Control id="buttonQuery" width="80" command="commandQuery" type="Button" value="查询"/> 
 <Control id="pagepilotEmployee" dataset="datasetEmployee" type="PagePilot"/> 
 <Control id="commandUpdate" type="UpdateCommand"> 
 <DatasetInfos> 
 <DatasetInfo dataset="datasetEmployee"/> 
 </DatasetInfos> 
 <Parameters/> 
 </Control> 
 <Control id="buttonSave" width="80" command="commandUpdate" type="Button" value="保存"/> 
 <Control id="dropdownSex" mapValue="true" type="ListDropDown"> 
 <DropDownItem label="<空>"/> 
 <DropDownItem label="男" value="true"/> 
 <DropDownItem label="女" value="false"/> 
 </Control> 
 <Control id="dropdownDept" mapValue="true" labelField="DEPT_NAME" dataset="datasetDept" type="DatasetDropDown" startWithEmptyRecord="true" valueField="DEPT_ID"/> 
 </Controls> 
 <Properties/> 
 </view> 


页面配置为:

<%@ page contentType="text/html; charset=UTF-8"%> 
<%@ taglib uri="http://www.bstek.com/dorado" prefix="d"%> 
 <html> 
 <head> 
 <title>sample</title> 
 </head> 
 <body scroll="no"> 
 <d:View config="com.docent.sample.sample_4_1"> 
 <d:Layout type="vflow"> 
 <d:Pane valign="top"> 
 <d:AutoForm id="formForm" /> 
 </d:Pane> 
 <d:Pane valign="top"> 
 <d:Layout type="hflow" width="100%"> 
 <d:Pane valign="top"> 
 <d:Button id="buttonSave" /><d:DataPilot id="datapilotEmployee" /> 
 </d:Pane> 
 <d:Pane align="right" valign="top"> 
 <d:PagePilot id="pagepilotEmployee" /> 
 </d:Pane> 
 </d:Layout> 
 </d:Pane> 
 <d:Pane valign="top" height="200"> 
 <d:DataTable id="tableEmployee" /> 
 </d:Pane> 
 </d:Layout> 
 </d:View> 
 </body> 
 </html>

ViewModel.java+view.xml+jsp

我们针对3.2.1的内容一步一步把一些用view.xml配置出来的内容通过ViewModel.java来实现。

  1. 我们先对该view.xml增加一个clazz:NewViewModel.java

<view clazz="com.docent.sample.Sample_4_1ViewModel">

NewViewModel.java可以对ViewModel进行处理的内容为:

package com.docent.sample;
import com.bstek.dorado.data.ParameterSet;
import com.bstek.dorado.view.DefaultViewModel;
import com.bstek.dorado.view.control.Control;
import com.bstek.dorado.view.data.ViewDataset;
public class Sample_4_1ViewModel extends DefaultViewModel {
    public void init(int state) throws Exception {
        // Add your code here
        super.init(state);
    }
    protected void initDataset(ViewDataset dataset) throws Exception {
        super.initDataset(dataset);
        // Add your code here
    }
    protected void initControl(Control control) throws Exception {
        super.initControl(control);
        // Add your code here
    }
    protected void initDatasets() throws Exception {
        super.initDatasets();
        // Add your code here
    }
    protected void initControls() throws Exception {
        super.initControls();
        // Add your code here
    }
    protected void doLoadData(ViewDataset dataset) throws Exception {
        // Add your code here
        super.doLoadData(dataset);
    }
    protected void doLoadData() throws Exception {
        // Add your code here
        super.doLoadData();
    }
    protected void doUpdateData(ParameterSet parameters,
            ParameterSet outParameters) throws Exception {
        // Add your code here
        super.doUpdateData(parameters, outParameters);
    }
}

现在我们把3.2.1中做出来的HelloWorld进行改造,把一些在view.xml定义的Datasets和Controls放到ViewModel.java里面定义。

  1. 把datasetForm的创建放到com.docent.sample. Sample_4_1ViewModel.java的init里

在该view.xml的view属性里设定clazz属性为:com.docent.sample.Sample_4_1ViewModel

datasetForm之前在view.xml的定义为:

把datasetForm的节点删掉,在Sample_4_1ViewModel.java的init(state)方法里面通过java代码来创建和设置属性。

public void init(int state) throws Exception { 
	FormDataset datasetForm = (FormDataset)this.createDataset("Form", "datasetForm"); 
	datasetForm.setAutoLoadData(false); 
	super.init(state); 
}
  1. 把datasetForm的属性设置和所有字段(Field)的创建放在initDataset(dataset)方法里创建

       protected void initDataset(ViewDataset dataset) throws Exception {
            super.initDataset(dataset);
            if ("datasetForm".equals(dataset.getId())
            // && (dataset instanceof FormDataset)
            ) {
                // dataset = (FormDataset)dataset;
                dataset.setInsertOnEmpty(true);
                Field fieldEMPLOYEE_ID = dataset.addField("EMPLOYEE_ID");
                fieldEMPLOYEE_ID.setLabel("员工编号");
                Field fieldDEPT_ID = dataset.addField("DEPT_ID");
                fieldDEPT_ID.setLabel("部门");
                if (this.getState() == ViewModel.STATE_VIEW) {
                    ((ViewField) fieldDEPT_ID).setDropDown("dropdownDept");
                }
                Field fieldEMPLOYEE_NAME = dataset.addField("EMPLOYEE_NAME");
                fieldEMPLOYEE_NAME.setLabel("员工姓名");
                Field fieldSEX = dataset.addField("SEX");
                fieldSEX.setLabel("性别");
            }
        }
  2. 把tableEmployee的创建放到com.docent.sample. Sample_4_1ViewModel.java的init里

     public void init(int state) throws Exception {
            FormDataset datasetForm = (FormDataset) this.createDataset("Form",
                    "datasetForm");
            datasetForm.setAutoLoadData(false);
            super.init(state);
            if (state == ViewModel.STATE_VIEW) {
                this.createControl("DataTable", "tableEmployee");
            }
        }
  3. 把tableEmployee的属性设置和所有字段(Column)的创建放在initControl(control)方法里创建

       protected void initControl(Control control) throws Exception {
            System.out.println(5);
            super.initControl(control);
            if (control instanceof DataTable) {
                if ("tableEmployee".equals(control.getId())) {
                    DataTable tableEmployee = (DataTable) control;
                    Dataset datasetEmployee = this.getDataset("datasetEmployee");
                    tableEmployee.setDataset(datasetEmployee.getId());
                    tableEmployee.setHeight("100%");
                    tableEmployee.setShowFilterBar(true);
                    tableEmployee.setShowHScrollBar(false);
                    tableEmployee.setWidth("100%");
                    Field selectField = datasetEmployee.getField("select");
                    tableEmployee.addColumnByField(selectField);
                    Field EMPLOYEE_IDField = datasetEmployee
                            .getField("EMPLOYEE_ID");
                    tableEmployee.addColumnByField(EMPLOYEE_IDField);
                    Field DEPT_IDField = datasetEmployee.getField("DEPT_ID");
                    tableEmployee.addColumnByField(DEPT_IDField);
                    Field EMPLOYEE_NAMEField = datasetEmployee
                            .getField("EMPLOYEE_NAME");
                    tableEmployee.addColumnByField(EMPLOYEE_NAMEField);
                    Field SEXField = datasetEmployee.getField("SEX");
                    tableEmployee.addColumnByField(SEXField);
                    Field BIRTHDAYField = datasetEmployee.getField("BIRTHDAY");
                    tableEmployee.addColumnByField(BIRTHDAYField);
                    // EditableColumn column =
                    // (EditableColumn)tableEmployee.getColumn("EMPLOYEE_ID");
                    // if(column !=null){
                    // EventHandler event = new EventHandler("onRefresh");
                    // String script = "cell.innerHTML = value;return false;";
                    // event.setScript(script);
                    // column.addEventHandler(event);
                    // }
                }
            }
        }

    页面配置跟3.2.1一样。

    ViewModel.java+jsp

    我们针对3.2.1的内容和3.2.2的实现方式来通过ViewModel.java加上Jsp实现跟它们同一个页面。
    我们新增一个ViewModel.java,该类继承于com.bstek.dorado.view.DefaultViewModel,具体实现代码(Sample_4_1_All_ViewModel.java)为:

    package com.docent.sample;
    import com.bstek.dorado.data.*;
    import com.bstek.dorado.data.db.*;
    import com.bstek.dorado.utils.collection.ObjectCollection;
    import com.bstek.dorado.utils.variant.*;
    import com.bstek.dorado.view.*;
    import com.bstek.dorado.view.control.*;
    import com.bstek.dorado.view.control.autoform.*;
    import com.bstek.dorado.view.control.command.*;
    import com.bstek.dorado.view.control.dropdown.*;
    import com.bstek.dorado.view.control.table.DataTable;
    import com.bstek.dorado.view.data.*;
    import com.bstek.dorado.view.data.validate.Validator;
    import com.bstek.dorado.view.data.validate.impl.RequiredValidator;
    public class Sample_4_1_All_ViewModel extends DefaultViewModel {
        public void init(int state) throws Exception {
            this.createDataset("Form", "datasetForm");
            this.createDataset("Wrapper", "datasetEmployee");
            this.createDataset("Wrapper", "datasetDept");
            super.init(state);
            if (state == ViewModel.STATE_VIEW) {
                this.createControl("ListDropDown", "dropdownSex");
                this.createControl("DatasetDropDown", "dropdownDept");
                this.createControl("QueryCommand", "commandQuery");
                this.createControl("Button", "buttonQuery");
                this.createControl("AutoForm", "formForm");
                this.createControl("PagePilot", "pagepilotEmployee");
                this.createControl("DataPilot", "datapilotEmployee");
                this.createControl("DataTable", "tableEmployee");
                this.createControl("UpdateCommand", "commandUpdate");
                this.createControl("Button", "buttonSave");
                EventHandler event = new EventHandler("onDatasetsPrepared");
                StringBuffer script = new StringBuffer();
                script.append("var record = datasetForm.getCurrent();");
                script.append("record.setValue(\"EMPLOYEE_ID\",\"ANLIN\");");
                event.setScript(script.toString());
                this.addEventHandler(event);
            }
        }
        protected void initDataset(ViewDataset dataset) throws Exception {
            super.initDataset(dataset);
            if (dataset instanceof FormDataset) {
                if ("datasetForm".equals(dataset.getId())) {
                    if (this.getState() == ViewModel.STATE_VIEW) {
                        dataset.setAutoLoadData(false);
                        dataset.setInsertOnEmpty(true);
                    }
                    Field fieldEMPLOYEE_ID = dataset.addField("EMPLOYEE_ID");
                    fieldEMPLOYEE_ID.setLabel("员工编号");
                    Field fieldEMPLOYEE_NAME = dataset.addField("EMPLOYEE_NAME");
                    fieldEMPLOYEE_NAME.setLabel("员工姓名");
                    Field fieldDEPT_ID = dataset.addField("DEPT_ID");
                    fieldDEPT_ID.setLabel("部门");
                    if (this.getState() == ViewModel.STATE_VIEW) {
                        ((ViewField) fieldDEPT_ID).setDropDown("dropdownDept");
                    }
                    Field fieldSEX = dataset.addField("SEX");
                    fieldSEX.setLabel("性别");
                }
            } else if (dataset instanceof DatasetWrapper) {
                if ("datasetEmployee".equals(dataset.getId())) {
                    dataset.setPageSize(10);
                    DatasetListener datasetListener = new EmployeeDatasetListener2();
                    dataset.addDatasetListener(datasetListener);
                    Variant v = new Variant(DataType.STRING);
                    dataset.parameters().setVariant("EMPLOYEE_ID", v);
                    DatasetWrapper datasetEmployeeDW = (DatasetWrapper) dataset;
                    datasetEmployeeDW.setWrappedType("AutoSql");
                    AutoSqlDataset datasetEmployee = (AutoSqlDataset) ((DatasetWrapper) dataset)
                            .getWrappedDataset();
                    datasetEmployee.setOriginTable("EMPLOYEE");
                    datasetEmployee.setKeyFields("EMPLOYEE_ID");
                    datasetEmployee.addDummyField("select");
                    AutoDBField field = (AutoDBField) datasetEmployee
                            .addField("EMPLOYEE_ID");
                    field.setTable("EMPLOYEE");
                    field.setOriginField("EMPLOYEE_ID");
                    field.setLabel("员工编号");
                    if (field instanceof ViewField) {
                        Validator validator = new RequiredValidator();
                        ((ViewField) field).addValidator(validator);
                    }
                    field = (AutoDBField) datasetEmployee.addField("DEPT_ID");
                    field.setTable("EMPLOYEE");
                    field.setOriginField("DEPT_ID");
                    field.setLabel("部门");
                    field = (AutoDBField) datasetEmployee.addField("EMPLOYEE_NAME");
                    field.setTable("EMPLOYEE");
                    field.setOriginField("EMPLOYEE_NAME");
                    field.setLabel("员工姓名");
                    field = (AutoDBField) datasetEmployee.addField("SEX");
                    field.setTable("EMPLOYEE");
                    field.setOriginField("SEX");
                    field.setLabel("性别");
                    if (field instanceof ViewField) {
                        ((ViewField) field).setDropDown("dropdownSex");
                    }
                    field = (AutoDBField) datasetEmployee.addField("BIRTHDAY");
                    field.setTable("EMPLOYEE");
                    field.setOriginField("BIRTHDAY");
                    field.setLabel("出生日期");
                    field.setDataType(DataType.DATETIME);
                    field = (AutoDBField) datasetEmployee.addField("MARRIED");
                    field.setTable("EMPLOYEE");
                    field.setOriginField("MARRIED");
                    field.setDataType(DataType.BOOLEAN);
                    field = (AutoDBField) datasetEmployee.addField("SALARY");
                    field.setTable("EMPLOYEE");
                    field.setOriginField("SALARY");
                    field.setDataType(DataType.DOUBLE);
                    field = (AutoDBField) datasetEmployee.addField("DEGREE");
                    field.setTable("EMPLOYEE");
                    field.setOriginField("DEGREE");
                    field = (AutoDBField) datasetEmployee.addField("EMAIL");
                    field.setTable("EMPLOYEE");
                    field.setOriginField("EMAIL");
                    field = (AutoDBField) datasetEmployee.addField("WEB");
                    field.setTable("EMPLOYEE");
                    field.setOriginField("WEB");
                    field = (AutoDBField) datasetEmployee.addField("CMNT");
                    field.setTable("EMPLOYEE");
                    field.setOriginField("CMNT");
                    BaseMatchRule baseMatchRule = datasetEmployee.addBaseMatchRule(
                            "EMPLOYEE_ID", "=", ":EMPLOYEE_ID");
                    baseMatchRule.setEscapeEnabled(true);
                    baseMatchRule.setLevel(1);
                    baseMatchRule.setTable("EMPLOYEE");
                    baseMatchRule = datasetEmployee.addBaseMatchRule(
                            "EMPLOYEE_NAME", "like", ":EMPLOYEE_NAME");
                    baseMatchRule.setEscapeEnabled(true);
                    baseMatchRule.setLevel(1);
                    baseMatchRule.setTable("EMPLOYEE");
                    baseMatchRule = datasetEmployee.addBaseMatchRule("DEPT_ID",
                            "=", ":DEPT_ID");
                    baseMatchRule.setEscapeEnabled(true);
                    baseMatchRule.setLevel(1);
                    baseMatchRule.setTable("EMPLOYEE");
                    baseMatchRule = datasetEmployee.addBaseMatchRule("SEX", "=",
                            ":SEX");
                    baseMatchRule.setEscapeEnabled(true);
                    baseMatchRule.setLevel(1);
                    baseMatchRule.setTable("EMPLOYEE");
                    baseMatchRule.setDataType(DataType.BOOLEAN);
                } else if ("datasetDept".equals(dataset.getId())) {
                    DatasetWrapper datasetDeptDW = (DatasetWrapper) dataset;
                    datasetDeptDW.setWrappedType("AutoSql");
                    AutoSqlDataset datasetDept = (AutoSqlDataset) ((DatasetWrapper) dataset)
                            .getWrappedDataset();
                    datasetDept.setOriginTable("DEPT");
                    AutoDBField field = (AutoDBField) datasetDept
                            .addField("DEPT_ID");
                    field.setTable("DEPT");
                    field.setOriginField("DEPT_ID");
                    field.setLabel("部门");
                    field = (AutoDBField) datasetDept.addField("BRANCH_ID");
                    field.setTable("DEPT");
                    field.setOriginField("BRANCH_ID");
                    field = (AutoDBField) datasetDept.addField("DEPT_NAME");
                    field.setTable("DEPT");
                    field.setOriginField("DEPT_NAME");
                }
            }
        }
        protected void initControl(Control control) throws Exception {
            System.out.println(5);
            super.initControl(control);
            if (control instanceof DataTable) {
                if ("tableEmployee".equals(control.getId())) {
                    DataTable tableEmployee = (DataTable) control;
                    DatasetWrapper datasetEmployeeDW = (DatasetWrapper) this
                            .getDataset("datasetEmployee");
                    Dataset datasetEmployee = datasetEmployeeDW.getWrappedDataset();
                    tableEmployee.setDataset(datasetEmployee.getId());
                    tableEmployee.setHeight("100%");
                    tableEmployee.setShowFilterBar(true);
                    tableEmployee.setShowHScrollBar(false);
                    tableEmployee.setWidth("100%");
                    Field selectField = datasetEmployee.getField("select");
                    tableEmployee.addColumnByField(selectField);
                    Field EMPLOYEE_IDField = datasetEmployee
                            .getField("EMPLOYEE_ID");
                    tableEmployee.addColumnByField(EMPLOYEE_IDField);
                    Field DEPT_IDField = datasetEmployee.getField("DEPT_ID");
                    tableEmployee.addColumnByField(DEPT_IDField);
                    Field EMPLOYEE_NAMEField = datasetEmployee
                            .getField("EMPLOYEE_NAME");
                    tableEmployee.addColumnByField(EMPLOYEE_NAMEField);
                    Field SEXField = datasetEmployee.getField("SEX");
                    tableEmployee.addColumnByField(SEXField);
                    Field BIRTHDAYField = datasetEmployee.getField("BIRTHDAY");
                    tableEmployee.addColumnByField(BIRTHDAYField);
                }
            } else if (control instanceof AutoForm) {
                if ("formForm".equals(control.getId())) {
                    AutoForm formForm = (AutoForm) control;
                    formForm.setDataset("datasetForm");
                    formForm.generateDefaultElements();
                    ObjectCollection groups = formForm.getGroups();
                    if (groups.size() > 0) {
                        FormGroup groupDefault = (FormGroup) groups.get(0);
                        groupDefault.setTitle("员工查询");
                        CustomFormElement customFormElement = (CustomFormElement) groupDefault
                                .addElement("Custom", "buttonQuery");
                        customFormElement.setControlId("buttonQuery");
                        customFormElement.setColSpan(2);
                        customFormElement.setShowLabel(false);
                        customFormElement.setControlAlign("right");
                    }
                }
            } else if (control instanceof Command) {
                if (control instanceof QueryCommand) {
                    if ("commandQuery".equals(control.getId())) {
                        QueryCommand commandQuery = (QueryCommand) control;
                        commandQuery.setConditionDataset("datasetForm");
                        commandQuery.setQueryDataset("datasetEmployee");
                        EventHandler event = new EventHandler("beforeExecute");
                        StringBuffer script = new StringBuffer();
                        script.append("var record = datasetForm.getCurrent();");
                        script.append("if(record){");
                        script.append("var EMPLOYEE_NAME = record.getValue(\"EMPLOYEE_NAME\");");
                        script.append("if(EMPLOYEE_NAME){");
                        script.append("EMPLOYEE_NAME = \"%\" + EMPLOYEE_NAME + \"%\";");
                        script.append("command.parameters().setValue(\"EMPLOYEE_NAME\",EMPLOYEE_NAME);");
                        script.append("}else{");
                        script.append("command.parameters().removeParameter(\"EMPLOYEE_NAME\");");
                        script.append("}");
                        script.append("}");
                        event.setScript(script.toString());
                        commandQuery.addEventHandler(event);
                    }
                } else if (control instanceof UpdateCommand) {
                    if ("commandUpdate".equals(control.getId())) {
                        UpdateCommand commandUpdate = (UpdateCommand) control;
                        commandUpdate.addDatasetInfo("datasetEmployee");
                    }
                }
            } else if (control instanceof Button) {
                if ("buttonQuery".equals(control.getId())) {
                    Button buttonQuery = (Button) control;
                    buttonQuery.setValue("查询");
                    buttonQuery.setWidth("80");
                    buttonQuery.setCommand("commandQuery");
                } else if ("buttonSave".equals(control.getId())) {
                    Button buttonSave = (Button) control;
                    buttonSave.setValue("保存");
                    buttonSave.setWidth("80");
                    buttonSave.setCommand("commandUpdate");
                }
            } else if (control instanceof PagePilot) {
                if ("pagepilotEmployee".equals(control.getId())) {
                    PagePilot pagepilotEmployee = (PagePilot) control;
                    pagepilotEmployee.setDataset("datasetEmployee");
                }
            } else if (control instanceof DropDown) {
                if (control instanceof ListDropDown) {
                    if ("dropdownSex".equals(control.getId())) {
                        ListDropDown dropdownSex = (ListDropDown) control;
                        dropdownSex.setMapValue(true);
                        dropdownSex.addItem(null, "<空>");
                        dropdownSex.addItem("true", "男");
                        dropdownSex.addItem("false", "女");
                    }
                } else if (control instanceof DatasetDropDown) {
                    if ("dropdownDept".equals(control.getId())) {
                        DatasetDropDown dropdownDept = (DatasetDropDown) control;
                        dropdownDept.setDataset("datasetDept");
                        dropdownDept.setStartWithEmptyRecord(true);
                        dropdownDept.setMapValue(true);
                        dropdownDept.setLabelField("DEPT_NAME");
                        dropdownDept.setValueField("DEPT_ID");
                    }
                }
            } else if (control instanceof DataPilot) {
                if ("datapilotEmployee".equals(control.getId())) {
                    DataPilot datapilotEmployee = (DataPilot) control;
                    datapilotEmployee.setDataset("datasetEmployee");
                }
            }
        }
        protected void initDatasets() throws Exception {
            super.initDatasets();
        }
        protected void initControls() throws Exception {
            super.initControls();
        }
        protected void doLoadData(ViewDataset dataset) throws Exception {
            super.doLoadData(dataset);
        }
        protected void doLoadData() throws Exception {
            super.doLoadData();
        }
        protected void doUpdateData(ParameterSet parameters,
                ParameterSet outParameters) throws Exception {
            super.doUpdateData(parameters, outParameters);
        }
    }



    页面配置为:

    <%@ page contentType="text/html; charset=UTF-8"%> 
    <%@ taglib uri="http://www.bstek.com/dorado" prefix="d"%> 
     <html> 
     <head> 
     <title>sample</title> 
     </head> 
     <body scroll="no"> 
     <d:View clazz="com.docent.sample.Sample_4_1_All_ViewModel"> 
     <d:Layout type="vflow"> 
     <d:Pane valign="top"> 
     <d:AutoForm id="formForm" /> 
     </d:Pane> 
     <d:Pane valign="top"> 
     <d:Layout type="hflow" width="100%"> 
     <d:Pane valign="top"> 
     <d:Button id="buttonSave" /><d:DataPilot id="datapilotEmployee" /> 
     </d:Pane> 
     <d:Pane align="right" valign="top"> 
     <d:PagePilot id="pagepilotEmployee" /> 
     </d:Pane> 
     </d:Layout> 
     </d:Pane> 
     <d:Pane valign="top" height="200"> 
     <d:DataTable id="tableEmployee" /> 
     </d:Pane> 
     </d:Layout> 
     </d:View> 
     </body> 
     </html> 

    从上面的页面配置信息来看,通过ViewModel.java+Jsp来实现的页面,跟之前的做法不同的地方只是在<d:view>的配置属性上,3.2.13.2.2需要配置config属性来指定哪个view.xml,3.2.2中的view.xml还配置了ViewModel.java,这里3.2.3需要配置clazz属性来指定哪个ViewModel.java。
    3.2.13.2.23.2.3的顺序来看,同一个页面,通过配置信息(view.xml)可以实现,通过配置信息(view.xml)和后台代码(ViewModel.java)也可以实现,通过后台代码(ViewModel.java)也一样可以实现。这里就说明了,配置view.xml其实就为了生成后台代码(ViewModel.java),只是通过配置的方式来实现java代码的编写。