一直看到dwr,项目框架中也用到这个东东, 但是一直没时间,也太懒,没去搞到底是啥,呵呵呵!
最近闲下来,看看dwr到底是什么东西,一看原来是个很简单的东西,dwr是:Direct Web Remoting, 其实就是个ajax框架!
看文库中的这个就可以完全看懂了!
这篇文章讲的也很详细
http://www.jb51.cc/article/p-yfbuvihe-ks.html
dwr的signatures下面这个文章讲的很详细
http://blog.csdn.net/geminiroy/article/details/5033758
我就把我的代码 及一些心得记录下,上面的文章都讲的超级详细 呵呵~
第一步 创建个 web项目,然后将dwr.jar放到项目的lib下面
第二步 配置web.xml 配置相应的servlet 添加下面的代码
<servlet> <display-name>DWR Servlet</display-name> <servlet-name>dwr-invoker</servlet-name> <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class> <init-param> <param-name>debug</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>crossDomainSessionSecurity</param-name> <param-value>false</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dwr-invoker</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping>
第三步 创建个WEB-INF 下面创建个 dwr.xml 文件
<?xml version="1.0" encoding="UTF-8"?> <!-- <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN" "http://directwebremoting.org/schema/dwr30.dtd"> --> <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://getahead.org/dwr/dwr20.dtd"> <dwr> <allow> <create creator="new" javascript="JDate"> <param name="class" value="java.util.Date" /> </create> <create creator="new" javascript="First"> <param name="class" value="com.ausky.dwr.controller.First" /> </create> <convert match="com.ausky.dwr.convert.MyBean" converter="bean"></convert> </allow> <signatures> <![CDATA[ import java.util.List; import com.ausky.dwr.controller.First; import com.ausky.dwr.convert.MyBean; First.testBeans(List<MyBean>); ]]> </signatures> </dwr>
第四步 创建相应的class文件
第五步 启动服务
第六步 访问
http://localhost:8080/dwr/
可以看到dwr配置了的 class
第七步 创建页面
Methods For: First (com.ausky.dwr.controller.First) To use this class in your javascript you will need the following script includes: <script type='text/javascript' src='/dwr/interface/First.js'></script> <script type='text/javascript' src='/dwr/engine.js'></script>
将上面2个js 加入到页面中 就可以访问了
第八步 就是具体的使用了 我把代码贴出来
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <Meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <script src='dwr/interface/First.js'></script> <script src='dwr/engine.js'></script> <script src='/dwr/interface/JDate.js'></script> <title>Insert title here</title> <script type="text/javascript"> /* First.test(function(str){alert(str);}); JDate.getTime(function(str){alert(str);}); */ /* First.testConvert(function(res){ for(var key in res){ alert("key:"+key+" val:"+res[key]); } }); */ /* First.testConvert(convert); function convert(obj){ for(var key in obj){ alert("key:"+key+" val:"+obj[key]); } } var data = ["1","2","3","4"]; First.testList(data,function(str){alert(str);}); */ var dataBeans = [ {name:"name1",address:"address1",age:11,sex:'m'},{name:"name1",sex:'m'}]; First.testBeans(dataBeans,function(str){alert(str);}); </script> </head> <body> 11111111111111111111 </body> </html>
我把 src跟webroot目录打包了, 需要的朋友可以下下来看看
(不知道怎么上传附件,只能放到资源里面了地址在下面)
原文链接:https://www.f2er.com/ajax/165257.html