SmartGWT学习整理 2、理解核心中的核心DataSource

SmartGWT学习整理 2、理解核心中的核心DataSource


DataSource之所以重要,是因为它负责所有的与服务器的数据交互,几乎所有的控件都离不开它。
可以这样说,理解了DataSource就掌握了SmartGWT的70%

DataSource支持的数据格式有xml、json、webservise等。他可以将数据按照指定的格式解析为可供控件使用的数据(Record对象),每一条数据就想数据库中的一行记录一样。
举个记录列表的例子来说,http://www.smartclient.com/smartgwt/showcase/#grid_databinding_xml_datasource
showcase中的这个例子就是将xml文件作为数据源,格式为
<List>

<country>
    <continent>North America</continent>
    <countryName>United States</countryName>
    <countryCode>US</countryCode>
    <area>9631420</area>
    <population>298444215</population>
    <gdp>12360000</gdp>
    <independence>1776-07-04</independence>
    <government>federal republic</government>
    <government_desc>2</government_desc>
    <capital>Washington, DC</capital>
    <member_g8>true</member_g8>
    <article>http://en.wikipedia.org/wiki/United_states</article>
</country>
...
...
...
</List>

每条country记录包括了continent、countryName等字段,那么对应的DataSource就是

private static class CountryDS extends DataSource {   
        // The DataSource would normally be defined external to any classes that use it.   
  
        private static CountryDS instance = null;   
           
        public static CountryDS getInstance() {   
            if (instance == null) {   
              instance = new CountryDS("countryDS_XML");   
            }   
            return instance;   
        }   
  
        public CountryDS(String id) {   
            setID(id);   
            setDataFormat(DSDataFormat.XML);   
            setRecordXPath("/List/country");   
            DataSourceField countryCodeField = new DataSourceField("countryCode", FieldType.TEXT,   
                                                                   "Code");   
            DataSourceField countryNameField = new DataSourceField("countryName", FieldType.TEXT,   
                                                                   "Country");   
            DataSourceField capitalField = new DataSourceField("capital", FieldType.TEXT,   
                                                               "Capital");   
            setFields(countryCodeField, countryNameField, capitalField);   
            setDataURL("ds/test_data/country.data.xml");   
        }   
  
    }  

showcase里的这个例子是通过继承DataSource来创建实例,一般也可以直接new DataSource()来创建。
这里用到了DataSource中的几个重要方法,
setDataFormat(DSDataFormat.XML);//这个方法声明了要调用的数据源是xml格式的
setRecordXPath("/List/country"); //声明了解析xml的数据路径,这里就是指在<List>标签下的<country>标签就是需要的数据
然后定义了3个字段DataSourceField
setFields方法设定DataSource中所有的字段。
setDataURL("ds/test_data/country.data.xml"); 这个方法指定了数据源的url地址,DataSource将从这个地址获取数据

数据字段DataSourceField的构造函数DataSourceField(String name, FieldType type, String title) 
name是数据内部使用的名字,FieldType是指定数据的类型,title是供控件显示给用户的名字

一般不直接使用DataSourceField,而是使用它的子类,都在com.smartgwt.client.data.fields包下,
例如字符型字段就用DataSourceTextField,布尔型用DataSourceBooleanField等等。

接下来定义一个列表控件(ListGrid)用来展示数据

final ListGrid countryGrid = new ListGrid();   
countryGrid.setWidth(400);   
countryGrid.setHeight(224);   
countryGrid.setShowAllRecords(true);   
countryGrid.setDataSource(CountryDS.getInstance());//将定义好的DataSource传递给列表控件
countryGrid.setAutoFetchData(true); //设定控件在显示的时候自动通过DataSource获取数据,如果设为false的话,那么需要手动通过countryGrid.fetch();方法来获取数据
countryGrid.draw();

这样一个显示国家数据的列表页面就完成了。

接下来再看一个json数据格式的例子
http://www.smartclient.com/smartgwt/showcase/#grid_databinding_json_datasource
里边的差别就是建立DataSource的时候通过setDataFormat(DSDataFormat.JSON);   
将数据源的格式指定为json格式,没有指定setRecordXPath是因为这个json数据里根节点的数据就是需要的数据。

可以看到用SmartGWT可以很方便的来展示数据,在MVC框架中可以用SmartGWT来画前台界面,后台只需要生成指定格式(xml或json,个人比较偏向使用json)的数据就可以了。

建了个smartGWT的qq群,有兴趣的可以进来一起学习讨论

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值