DWR使用实例

DWR
非常好用的AJAX的使用架构,过程是通过dwr包将写好了的java方法转换成js可以处理的输出方式,然后在js方法中进行调用,组织输出
配置步骤:
http://blogger.org.cn/blog/more.asp?name=lhwork&id=21045
配置完成后可以查看测试页。
使用步骤:
1.写好自己的类和其中方法。
2.将java的累(action)在dwr的相关配置文件中注册(dwr.xml)。
如;

<dwr> 
  <allow> 
<create creator="new" javascript="AjaxAction">//引用的l类文件,即将来生成的js方法名 
<param name="class" value="cn.univs.action.event.AjaxAction" />//引入之前写好的,用于ajax应用的类。 
<include method="getCity"/>//引入使用的方法名 
<include method="isPollTime"/> 
<include method="isVoteRange"/> 
<include method="isVoteLimit"/> 
<include method="worksPoll"/> 
</create> 
  </allow> 
</dwr> 

 
3.在前台页面引入dwr开发包,机器相关工具类。
如: 

 <script type='text/javascript' src='/univs/dwr/interface/AjaxAction.js'></script>  
<script type='text/javascript' src='/univs/dwr/engine.js'></script> 
<script type='text/javascript' src='/univs/dwr/util.js'></script> 

 
要注意src的路径。

4.写相关的js方法。
如:

<script type="text/javascript" > 
function showunivs(provinceid){ 
AjaxAction.getCity(provinceid,showCtnt);//前台通过动作来触发此函数,可以看到AjaxAction是类的名称,getCity是其中方法的名称。 
} 
function showCtnt(list){ 
try{ 
var ctnt=""; 
for(var i=0;i<list.length;i++){ 
var city=list[i]; 
var pos=city.indexOf("&"); 
var ctid=city.substr(0,pos); 
var ctname=city.substr(pos+1,city.length); 
ctnt+="<li><a href='#' onclick=retnVal("+ctid+",'"+ctname+"')>"+ctname+"</a></li>"; 

} 
DWRUtil.setValue("ctnt",ctnt);//DWRUtil是dwr的开发工具包,setValue方法可以直接给页面中id=ctnt的位置赋值。 
}catch(ex){alert(ex.description);} 
} 

function retnVal(id,name){ 
if(document.all){//ie 
window.returnValue=id; 
}else{ 
//mozilla 
window.opener.rVal=id; 
window.opener.ctflush(); 
} 
window.close(); 
} 
</script> 

 
使用中可以通过测试也来查看java方法的正确性 如http://localhost:8080/xxx/dwr/test/AjaxAction

 

你可能感兴趣的:(JavaScript,Ajax,DWR,IE,asp)