DWR 在项目中的应用
Posted zhuanzhuan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DWR 在项目中的应用相关的知识,希望对你有一定的参考价值。
- DWR框架简介
- DWR框架是一个可以允许你去创建 AJAX WEB站点的JAVA开源库。它可以让你在浏览器的javascript代码中调用Web服务器的Java代码,就像Java代码在浏览器中一 样。DWR工作原理是通过动态把Java类生成JavaScript,让使用者感觉调用就像发生在浏览器端。
- 2、DWR的使用场合
- 当我们的业务需要在页面不提交的情况下访问服务器端并实现页面数据局部刷新时,我们就可以使用DWR。
- 1、导入dwr的jar包
- 2、编写一个dwr框架在页面中用JavaScript要调用到的java类。
- web.xml中的配置
- <servlet>
<servlet-name>dwr-invoker</servlet-name>
<display-name>DWR Servlet</display-name>
<description>Direct Web Remoter Servlet</description>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
<!-- This should NEVER be present in live -->
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<!-- Remove this unless you want to use active reverse ajax -->
<init-param>
<param-name>activeReverseAjaxEnabled</param-name>
<param-value>true</param-value>
</init-param>
<!-- By default DWR creates application scope objects when they are first
used. This creates them when the app-server is started -->
<init-param>
<param-name>initApplicationScopeCreatorsAtStartup</param-name>
<param-value>true</param-value>
</init-param>
<!-- This enables full streaming mode. It‘s probably better to leave this
out if you are running across the internet -->
<init-param>
<param-name>maxWaitAfterWrite</param-name>
<param-value>-1</param-value>
</init-param>
<!--
For more information on these parameters, see:
- http://getahead.org/dwr/server/servlet
- http://getahead.org/dwr/reverse-ajax/configuration
-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping> - Dwr.xml
- <dwr>
<allow>
<!--
<filter class="org.getahead.dwrdemo.monitor.MonitoringAjaxFilter"/>
<filter class="org.directwebremoting.filter.ExtraLatencyAjaxFilter">
<param name="delay" value="200"/>
</filter>
-->
<!-- intro - for the test on index.html -->
<create creator="new" javascript="Intro">
<param name="class" value="org.getahead.dwrdemo.intro.Intro"/>
</create>
<convert match="com.dianzhi.bean.City" converter="bean">
<param name="include" value="cityId,cityName" />
</convert>
<!-- simpletext -->
<create creator="new" javascript="DWRService">
<param name="class" value="com.dianzhi.DWRService"/>
</create>
</allow>
</dwr> - 5、编写jsp页面,用javascript调用后台的java代码
- <script type=‘text/javascript‘
src=‘/dwr_mycity/dwr/interface/DWRService.js‘>
</script>
<script type=‘text/javascript‘ src=‘/dwr_mycity/dwr/engine.js‘>
</script>
<script type=‘text/javascript‘ src=‘/dwr_mycity/dwr/util.js‘>
</script>
<script type="text/javascript" src="jquery/jquery-1.3.2.js">
</script>
<script type="text/javascript">
window.onload=function(){
DWRService.getPropive(function(data){
for(var p in data){
//alert(p);
// alert(data[p]);
var option = document.createElement("option");
option.innerHTML=data[p];
option.value=p;
document.getElementById("provice").appendChild(option);
}
});
}
function loadshi(id){
//alert("aaaa");
DWRService.getCity(id,function(data){
document.getElementById("city").innerHTML="" ;
document.getElementById("qu").innerHTML="" ;
for(var c in data){
var option =document.createElement("option");
option.innerHTML=data[c];
option.value=c;
document.getElementById("city").appendChild(option);
}
});
}
function loadqu(id){
//alert("aaaa");
DWRService.getQu(id,function(data){
document.getElementById("qu").innerHTML="" ;
for(var q in data){
var option =document.createElement("option");
option.innerHTML=data[q];
option.value=q;
document.getElementById("qu").appendChild(option);
}
});
}
</script>
</head>
<body>
<select id="provice" onchange="loadshi(this.value)">
</select>
<select id="city" onchange="loadqu(this.value)">
</select>
<select id="qu">
</select>
</body>
</html> - public class DWRService {
/*public List<City> getPropive (){
ICityDao dao = new CityDao();
List<City> list =dao.findByParentId(0);
for (City city : list) {
System.out.println(city.getCityId()+"..."+city.getCityName());
}
System.out.println(list.size());
return list;
}*/
public Map<Integer,String> getPropive (){
ICityDao dao = new CityDao();
List<City> list =dao.findByParentId(0);
Map<Integer,String> map = new HashMap<Integer,String>();
for (City city : list) {
//System.out.println(city.getCityId()+"..."+city.getCityName());
map.put(city.getCityId(), city.getCityName());
}
return map;
}
public Map<Integer,String> getCity(int id){
ICityDao dao = new CityDao();
List<City> list =dao.findByParentId(id);
Map<Integer,String> map = new HashMap<Integer,String>();
for (City city : list) {
//System.out.println(city.getCityId()+"..."+city.getCityName());
map.put(city.getCityId(), city.getCityName());
}
return map;
}
public Map<Integer,String> getQu(int id){
ICityDao dao = new CityDao();
List<City> list =dao.findByParentId(id);
Map<Integer,String> map = new HashMap<Integer,String>();
for (City city : list) {
//System.out.println(city.getCityId()+"..."+city.getCityName());
map.put(city.getCityId(), city.getCityName());
}
return map;
}
- 5、编写jsp页面,用javascript调用后台的java代码
以上是关于DWR 在项目中的应用的主要内容,如果未能解决你的问题,请参考以下文章