最近在做一个物流的管理系统用于对以前学习的SSH和其他的知识复习巩固一下,顺便了解一下SHH和DWR的最新功能:
该实现是没有封装的,可以增加同类的js和配置后,可以无限的联动下去
1.SHH和DWR的框架搭建就不在详细描述,大家可以在google找到N多!
2.hibernate的实体及配置:
在cn.com.pumkinchina.mapping下建类
public class Province {
//省ID
private Long provinceid;
//省名
private String provincename;
//get和set方法省略
}
public class City {
//市ID
private Long cityid;
//市名称
private String cityname;
。。。 。。。
//get和set方法省略
}
在同一位置创建映射文件
Province.hbm.xml
<hibernate-mapping>
<class name="cn.com.pumkinchina.mapping.Province" table="C_Province">
<id name="provinceid">
<generator class="native" />
</id>
<property name="provincename"/>
</hibernate-mapping>
City.hbm.xml
<hibernate-mapping>
<class name="cn.com.pumkinchina.mapping.City" table="C_Province">
<id name="cityid">
<generator class="native" />
</id>
<property name="cityname"/>
</hibernate-mapping>
2.查询逻辑
在cn.com.pumkinchina.service.dwr包下建立
public class ProvinceAndCityLiandong {
//获得省的列表
public List<Province> getProvinceList() {
return provinceService.getAll();
}
//根据省的id查询市的列表
public List<City> getCityList(String provinceid) {
if(provinceid == null || provinceid.trim().length()==0) {
return cityService.getCityAll();
}
return cityService.getCityrForProvince(provinceid);
}
/**
* IOC
*/
private ProvinceService provinceService;
private CityService cityService;
//省略set的设置器注入方法
}
3.在config包下建立spring关于dwr服务的配置文件applicationContext-dwr.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="ProvinceAndCityService" class="cn.com.pumkinchina.service.dwr.ProvinceAndCityLiandong" >
<property name="provinceService" ref="ProvinceService" />
<property name="cityService" value="CityService" />
</bean>
</beans>
4.WEB-INF下建立dwr.xml
<!DOCTYPE dwr PUBLIC
"-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN"
"http://directwebremoting.org/schema/dwr30.dtd">
<dwr>
<allow>
<convert converter="bean" match="cn.com.pumkinchina.mapping.Province">
<param name="include" value="provinceid,provincename" />
</convert>
<convert converter="bean" match="cn.com.pumkinchina.mapping.City">
<param name="include" value="cityid,cityname" />
</convert>
<create creator="spring" javascript="ProvinceAndCityScript">
<param name="beanName" value="ProvinceAndCityService"/>
<include method="getProvinceList"/>
<include method="getCityList"/>
</create>
</allow>
</dwr>
5.jsp:test.jsp
<%@ page contentType="text/html;charset=utf-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>框架页面</title>
<script type="text/javascript" src="<%=basePath%>dwr/interface/ProvinceAndCityScript.js"></script>
<script type="text/javascript" src="<%=basePath%>dwr/engine.js"></script>
<script type="text/javascript" src="<%=basePath%>dwr/util.js"></script>
<script type="text/javascript" src="<%=basePath%>jquery/jquery.js"></script>
<script type="text/javascript">
$(function(){
dwr.engine.setAsync(false);
getProvinceList();
getCityList();
});
function getProvinceList() {
ProvinceAndCityScript.getProvinceList({
callback:function(provincelist){
dwr.util.removeAllOptions("province");
dwr.util.addOptions("province",[{name:"请选择...",value:"0" }],"value","name");
dwr.util.addOptions("province",provincelist,"provinceid","provincename");
},
timeout:5000
});
}
function getCityList() {
ProvinceAndCityScript.getCityList(dwr.util.getValue("province"),{
callback:function(citylist){
dwr.util.removeAllOptions("city");
dwr.util.addOptions("city",[{name:"请选择...",value:"0" }],"value","name");
dwr.util.addOptions("city",citylist,"cityid","cityname");
},
timeout:5000
});
}
</script>
</head>
<body>
<select id="province" onchange="getCityList()"></select>
<select id="city"></select>
</body>
</html>