昨天和今天早上,用DataSource从数据库后台动态加载数据,我的业务是这样的:
我有两个SelectItem选择框,第一个选择框里面的数据是单位,第二个选择框中的数据是对应单位的人,因为人可能有重名,所以还需要加载对应的人的其他信息(如身份证等等),来个图:
(选择好之后的样子)
第一个选择框中的单位变了,第二的选择框中只加载对应单位的人的数据,但是关键问题出来了,我加载的数据每一次都不会改变,不管第一个选择框的选择的是什么,第二个选择框只能加载一次数据,就是初始化那一次,我尝试了将对应的DataSource重新初始化,甚至写一个匿名的DataSource,问题都没有解决,最后在各种查文档、各种实践之后,我才发现,原来问题在于DataSource内的一个方法:setID(String id),只要id不变,不管你怎么折腾,数据还是那些数据,所以我果断将DataSource的id换了,奇迹就出现了,数据重新加载了,下面附上关键代码:
DataSource类:
public class TobaLeafGridFarmerInfoXmlDs extends DataSource {
private String townId=null;
private String villageId=null;
private static TobaLeafGridFarmerInfoXmlDs instance=null;
public static TobaLeafGridFarmerInfoXmlDs getInstance(String townId,String villageId){
if(instance==null){
instance=new TobaLeafGridFarmerInfoXmlDs("TobaLeafGridFarmerData",townId,villageId);
}
return instance;
}
public TobaLeafGridFarmerInfoXmlDs(String id,String townId,String villageId){
setTownId(townId);
setVillageId(villageId);
setID(id);//此处的id很重要的,相同的id返回的数据是一样的
setTitleField("farmerName");
setRecordXPath("/List/TobaLeafGridFarmerData");
DataSourceTextField townNameField = new DataSourceTextField("townName","乡镇",70,true);
DataSourceTextField villageIdField = new DataSourceTextField("villageId","村委会Id",30,true);
villageIdField.setHidden(true);
DataSourceTextField villageField = new DataSourceTextField("village","村委会",100,true);
DataSourceTextField farmerNameField = new DataSourceTextField("farmerName","烟农",50,true);
DataSourceTextField computerCodeField = new DataSourceTextField("computerCode","合同号",80,true);
DataSourceTextField IdCardField = new DataSourceTextField("idCard","身份证",80,true);
setFields(townNameField,villageIdField,villageField,farmerNameField,computerCodeField,IdCardField);
setDataFormat(DSDataFormat.XML);
//下面这个URL是一个Servlet的URL,数据来源是从数据库中取数的
setDataURL(HOST + "analyse/farmerDataServlet"+"?townId="+townId+"&villageId="+(villageId==null?"":villageId));
setClientOnly(true);
setCacheAllData(false);
}
public String getTownId() {
return townId;
}
public void setTownId(String townId) {
this.townId = townId;
}
public String getVillageId() {
return villageId;
}
public void setVillageId(String villageId) {
this.villageId = villageId;
}
}
选择框部分代码(selectVillage为第一个选择框,selectFarmer为第二个选择框,都是SelectItem实例):
DataSource farmerXmlDS=new TobaLeafGridFarmerInfoXmlDs(townId,townId,"");
final ListGrid pickListProperties = new ListGrid();
pickListProperties.setShowFilterEditor(true);
ListGridField townName = new ListGridField("townName",70);
ListGridField villageId = new ListGridField("villageId",30);
villageId.setHidden(true);
ListGridField village = new ListGridField("village",120);
ListGridField farmerName = new ListGridField("farmerName",50);
ListGridField computerCode = new ListGridField("computerCode",90);
ListGridField idCard = new ListGridField("idCard",120);
selectFarmer.setPickListFields(townName,villageId,village,farmerName,computerCode,idCard);
selectFarmer.setOptionDataSource(farmerXmlDS);
selectFarmer.setDisplayField("farmerName");
selectFarmer.setValueField("computerCode");
selectFarmer.setPickListWidth(460);
selectFarmer.setPickListProperties(pickListProperties);
selectVillage.addChangeHandler(new ChangeHandler() {
public void onChange(ChangeEvent event) {
String selectedVillageId = (String) event.getValue();
DataSource farmerXmlDS=new TobaLeafGridFarmerInfoXmlDs(
selectedVillageId==null?townId:selectedVillageId,townId,selectedVillageId);
selectFarmer.setOptionDataSource(farmerXmlDS);
selectFarmer.redraw();
}
});
servlet关键代码:
@Override
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String xmlstring = "";
User user = WebRalasafe.getCurrentUser(request);
String townId=request.getParameter("townId");
String villageId=request.getParameter("villageId");
try {
xmlstring = getFarmerInfoXml(townId,villageId);
} catch (Exception e) {
e.printStackTrace();
}
response.setContentType("text/xml;charset=utf-8");
response.getWriter().write(xmlstring.toString());
}
public String getFarmerInfoXml(String townId,String villageId) throws Exception{
String farmerInfoXml="";
String sql="select …"; //sql略
List<String[]> result = xxx.QuerySql(sql); //自己写的执行sql的方法
/*TobaLeafGridFarmerData是一个模型类(继承com.google.gwt.user.client.rpc.IsSerializable接口,可序列化),有一堆属性和对应getter和setter方法,没有其他多余的内容*/
List<TobaLeafGridFarmerData> datas=new ArrayList<TobaLeafGridFarmerData>();
for(int i=0;i<result.size();i++){
String tmp[]=result.get(i);
TobaLeafGridFarmerData data=new TobaLeafGridFarmerData();
data.setTownName(tmp[0]);
data.setVillageId(tmp[1]);
data.setVillage(tmp[2]);
data.setFarmerName(tmp[3]);
data.setComputerCode(tmp[4]);
data.setIdCard(tmp[5]);
datas.add(data);
}
String rootElement = "List";
boolean isOutputToFile = false;
String outFilePath = "";
farmerInfoXml = Xmloutput.object2Xml(rootElement, datas, isOutputToFile,
outFilePath);
return farmerInfoXml;
}
/*大概思路:首先DataSource(通过servlet)加载数据,同时要定义对应元数据(上面的是TobaLeafGridFarmerData),然后SelectItem再调用这个DataSource,就这样*/