1、导入dwr.jar包,加入到WEB-INF/lib
2、在web.xml中加入
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/dwr.xml</param-value>
</init-param>
<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>
3、写个JS要调用的类
package com.across.dwr;
public class categoryRead {
public JSONObject getDesks(int deskclassid)throws UserDefineException {
TableManager tableManager = new TableManager();
tableManager.open();
Hashtable htData = null;
Vector vResult = null;
JSONObject obj = new JSONObject();
JSONArray arr = new JSONArray();
try {
String sSql = "select * from desk where deskclass_id="+deskclassid;
tableManager.setSqlStatement(sSql);
vResult = tableManager.executeSqlQuery();
if (vResult != null) {
for (int i = 0; i < vResult.size(); i++) {
htData = (Hashtable) vResult.elementAt(i);
JSONObject bj = new JSONObject();
bj.put("oV", htData.get("DESK_ID"));
bj.put("oT", htData.get("DESK_NAME"));
arr.add(bj);
}
}
obj.put("datas", arr);
obj.put("count", vResult.size());
obj.put("isOK", true);
obj.put("parentid", deskclassid);
return obj;
} catch (Exception udex) {
/* 抛出该方法异常 */
} finally {
/* 关闭数据库连接 */
tableManager.close();
}
}
}
4、dwr.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC
"-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN"
"http://getahead.ltd.uk/dwr/dwr20.dtd">
<dwr>
<allow>
<create creator="new" javascript="categoryRead">
<param name="class" value="com.across.dwr.categoryRead"/>
</create>
</allow>
</dwr>
5、jsp页面:
<%@ page language="java" pageEncoding="UTF-8"%>
<html>
<head>
<title>list.jsp</title>
<script language="javascript" src="../js/selectCombo.js"></script>
<script type="text/javascript" src="../dwr/interface/categoryRead.js"></script>
<script type="text/javascript" src="../dwr/engine.js"></script>
</head>
<body>
<html:select property="DESKCLASS_ID" styleId="select1">
<option value="0">---请选择---</option>
<html:options collection="DESKCLASSLIST" labelProperty="label" property="value" />
</html:select>
<html:select property="DESK_ID" styleId="select2">
<option value="0">---请选择---</option>
<html:options collection="DESKLIST" labelProperty="label" property="value" />
</html:select>
</body>
</html>
6、selectCombo.js文件:
$().ready(function() {
//select联动效果
window.areaTO;
$.fn.selectCombo=function(url,target,settings){
var defaults={hidetarget: true,indicator: false,pageload: false};
$.extend(defaults,settings);
return this.each(function(){
var qobj=this;
var targetlabel=target.replace(/#/,'');
targetlabel="label[@for='"+targetlabel+"']";
if(defaults.indicator!=false)
$(defaults.indicator).hide();
hidetargetinfo=function(){
if($(qobj).attr('id')!=$(target).attr('id')){
$(targetlabel).hide();
$(target).hide();
}}
if(defaults.hidetarget && $(target).val()=='')
hidetargetinfo();
loadOptions=function(){
qval=$(qobj).val();
if(defaults.indicator!=false) $(defaults.indicator).show();
if(defaults.hidetarget)
hidetargetinfo();
if($(qobj).attr('id')!=$(target).attr('id'))
$(target).empty();
if(url=='readArea'){
$(target).html('<option value="">列表载入 ...</option>');
clearTimeout(window.areaTO);
var that=$(this);
window.areaTO=setTimeout(function(){
var s=$(that).val();
if(s==""){
$(target).html('<option value="0">---请选择---</option>');
return false;
}
//alert(s);
categoryRead.getDesks(s,CBTAB);//调用后台!!!!!!!!!!!!!!!
},400);
}
function CBTAB(d){
if(d.isOK){
var ddd=d.datas;
var parent = d.parentid;
var strTmp='';
for(var i=0;i<d.count;i++){
strTmp+='<option value="'+ddd[i].oV+'">'+ddd[i].oT+'</option>';
}
strTmp+='<option value="0" selected="selected">---请选择---</option>';
$(target).html(strTmp);
if(defaults.indicator!=false) $(defaults.indicator).hide();
$(targetlabel).show();
$(target).show();
}else{
$(target).html('<option value="">无对应类型</option>');
}
}
}
$(this).change(loadOptions);
if(defaults.pageload && $(this).val()!=''){loadOptions();}});
}
/*js实现相关功能*/
$("#select1").selectCombo('readArea','#select2',{hidetarget:false,indicator:'#loading'});
});