dwr 获取数组列表对象,列表里的对象是数据库持久层的对象Attribute,结果在视图层,获取Attribute的值报错,在页面提示 data is null。
根据报错信息,发现Attribute对象内的映射关系迟缓加载有问题,实例如下:
js代码:
function getAttributeSupListJ(attributePath,attributeId,newAttributeId){
attributeDwr.getAttributeSupList(attributePath+"."+newAttId,__doCreateAttributeList);
}
function __doCreateAttributeList(data){
if(!data) return;
if(data.length<1) return;
var obj=document.getElementById("att_group_1");
var mySelect = document.createElement("select");
mySelect.id = new_att_group_1;
if(window.addEventListener) // Mozilla, Netscape, Firefox
{
mySelect.addEventListener('change', getAttributeSupListJ,false);//添加事件
}
else// IE
{
mySelect.attachEvent('onchange',getAttributeSupListJ);//添加事件
}
obj.appendChild(mySelect);
if(obj){
//DWRUtil.removeAllOptions(new_att_group_1);
DWRUtil.addOptions(new_att_group_1,data,'catalogId','catalogName');
}
}
DWR接口定义:
public List getAttributeSupList(String attributePath)
DWR实现类定义:
public List getAttributeSupList(String attributePath){......}
解决办法:
1、把Attribute.hbm.xml 的映射关系lazy="true" 改成lazy="false" :
<set name="productAttributeValues" table="product_attribute_value" lazy="false" inverse="true" sort="unsorted">
<key>
<column name="attributeId"/>
</key>
<one-to-many class="com.dingDang.mall.common.model.catalog.ProductAttributeValue"/>
</set>
2、新建一个临时存储Attribute 对象数据的 对象:
AttributeVO {
private Integer catalogId;
private String catalogName;
private String path;//
private String pathName;
private String data;
private String flag;
private Object object;
修改 DWR实现类定义:
public List getAttributeSupList(String attributePath){
List list = (List)attributeMenuUtil.getAttributesMap().get(attributePath);
List results=new ArrayList();
if(list!=null && list.size()>0){
AttributeVo attributeVo= null;
Attribute att = null;
for(int i=0;i<list.size();i++){
attributeVo= new AttributeVo ();
att = (Attribute)list.get(i);
attributeVo.setCatalogId(att.getAttributeId());
attributeVo.setCatalogName(att.getAttributeName());
attributeVo.setPath(att.getAttributePath());
attributeVo.setData(att.getDefaultValue());
attributeVo.setFlag(att.getInputType()+"");
results.add(i, attributeVo);
}
}
return results;
}