这几天学习dwr,最想实现的就是下拉框的联动功能吧,我想用dwr2结合ssh。网上关于多级联动的例子好多都不是我想要的,不过编程重要的是思想,学习别人的思想是非常重要的。“前人栽树,后人乘凉”,经过一天的努力终于完成了。
以下是我粘贴的部分代码,具体详细的请下载我的项目。
<script type='text/javascript' src='dwr/interface/DWRshengDao.js'></script>
<script type='text/javascript' src='dwr/interface/DWRshiDao.js'></script>
<script type='text/javascript' src='dwr/engine.js'></script>
<script type='text/javascript' src='dwr/util.js'></script>
<script type="text/javascript">
function getProvince(){
DWRshengDao.getShengMap(setProvince);
}
function setProvince(data){
var shengObj=document.getElementById("sheng");
DWRUtil.removeAllOptions(shengObj);
DWRUtil.addOptions(shengObj,{'':'请选择'});
DWRUtil.addOptions(shengObj,data);
}
function getCity(value){
DWRshiDao.getShiList(value,setCity);
//或以下的getShiMap
//DWRshiDao.getShiMap(value,setCity);
}
function setCity(data){
var shiObj=document.getElementById("shi");
DWRUtil.removeAllOptions(shiObj);
DWRUtil.addOptions(shiObj,{'':'请选择'});
DWRUtil.addOptions(shiObj,data);
}
function save(){
document.form1.action="addshi.action";
}
</script>
</head>
<body onload="getProvince()">
<table>
<form method="post" name="form1" id="form1">
<select id="sheng" onchange="getCity(this.value)"></select>
<select id="shi"></select>
<div><input type="submit" value="保存" onClick="save();"/>
</form>
其实<select>里id为A,为B什么的无所谓,只要在var XX=document.getElementById("A或B");对应上就行。
2、ShengDaoImpl.java
public Map getShengMap() {
log.debug("getting Sheng instance");
Map<String, String> map=new HashMap<String, String>();
try {
String query = "from Sheng";
List list = (List) getHibernateTemplate().find(query);
log.debug("get Sheng");
for(int i=0;i<list.size();i++){
Sheng sheng=(Sheng)list.get(i);
map.put(sheng.getShengid(),sheng.getShengname());
}
}
catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
return map;
}
3、ShiDaoImpl.java
public Map getShiMap(java.lang.String id) {
log.debug("getting Shi instance");
Map<String, String> map=new HashMap<String, String>();
try {
String query = "from Shi as s where s.sheng.shengid=?";
//注意是s.sheng.shengid而不是s.shengid
List list = (List) getHibernateTemplate().find(query,id);
log.debug("get Shi");
for(int i=0;i<list.size();i++){
Shi shi=(Shi)list.get(i);
map.put(shi.getShiid(),shi.getShiname());
}
}
catch (RuntimeException re) {
log.error("get Shi failed", re);
throw re;
}
return map;
}
4、web.xml
<!-- DWR servlet 配置 -->
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>
org.directwebremoting.spring.DwrSpringServlet
</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
注意是DwrSpringServlet而非DwrServlet
5、applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.directwebremoting.org/schema/spring-dwr
http://www.directwebremoting.org/schema/spring-dwr-2.0.xsd">
<!-- DWR 配置开始 -->
<dwr:configuration></dwr:configuration><!-- 必须要configuration -->
<!-- DWR 配置结束 -->
<bean id="shengDao" class="nn.liandong.jiguan.dao.ShengDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
<dwr:remote javascript="DWRshengDao"></dwr:remote>
</bean>
<bean id="shiDao" class="nn.liandong.jiguan.dao.ShiDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
<dwr:remote javascript="DWRshiDao"></dwr:remote>
</bean>
有了这个配置就不需要在dwr.xml里配置了。
为了正常运行本项目,除了下载liandong.rar这个文件外,请到“文章管理”里的“lib”里下载sshlib这两个压缩包!
本文介绍如何使用 DWR 结合 SSH 实现多级下拉框联动功能,包括 JavaScript 和 Java 代码示例。

581

被折叠的 条评论
为什么被折叠?



