1.如果要往页面传入一个对象时,在dwr.xml中的添加对象转换器,具体代码如下:
<allow>
<!-- creator="创建器"(new:UserDaoImpl dao = new UserDaoImpl()),javascript="产生的javascript名称" -->
<create creator="new" javascript="JUser">
<param name="class" value="com.xasxt.dao.impl.UserDaoImpl"></param>
<include method="checkUserName"/>
</create>
<create creator="new" javascript="JProvince">
<param name="class" value="com.xasxt.dao.impl.ProvinceDaoImpl"></param>
<include method="getAllProvinces"/>
<include method="getCitiesByProvinceId"/>
</create>
<!-- bean转换器:java对象json之间相互转化 -->
<convert converter="bean" match="com.xasxt.entity.Province"></convert>
<convert converter="bean" match="com.xasxt.entity.City"></convert>
</allow>
2后台的操作:
public List<Province> getAllProvinces(){
List<Province> list = new ArrayList<Province>();
String sql="select * from province";
try {
rs = this.query(sql);
while(rs.next()){
Province province = new Province();
province.setProvinceId(rs.getInt("provinceId"));
province.setProvinceName(rs.getString("provinceName"));
list.add(province);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
this.close();
}
return list;
}
将数据库查到的Provice放到list集合中;
3。页面取list中的类容:
<script type='text/javascript' src='/dwrDemo/dwr/engine.js'></script>
<script type='text/javascript' src='/dwrDemo/dwr/interface/JProvince.js'></script>
<script type='text/javascript' src='/dwrDemo/dwr/util.js'></script>
<script type="text/javascript">
function loadProvinces(){
JProvince.getAllProvinces(callBack);
}
function callBack(data){
<!--addOptions("表单中的id",返回的数据,"添加的ID","查询出每一条的Name")-->
dwr.util.addOptions("province",data,"provinceId","provinceName");
}
}
</script>