一、使用DWR开发第一个Hello World Demo的步骤
1、下载DWR开发发包,建议下载ZIP的压缩包,方便程序员学习。我使用的DWR版本是:dwr-2.0.3-src
2、新建web工程,在WEB-INF/lib下引入dwr.jar包, 添加dwr.xml文件(在DWR ZIP中有类似的dwr.xml文件可以COPY去掉没有用的内容即可)
3、在web.xml中加入如下配置:
<!-- 配置DWR -->
<servlet>
<display-name>DWR2.0.3 SERVELET</display-name>
<servlet-name>dwr-invoke</servlet-name>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
<init-param>
<description>打开调试模式,程序发布时必须关闭</description>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoke</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
4、写一个简单的JavaBean,如下:
package com.brofe.dwr.pojo;
public class RemotePojo {
public RemotePojo() {}
public String remoteGetStr () {
return "你好,brofe。 欢迎!!!";
}
}
5、在dwr.xml文件添加如下配置(一下是完整的xml文件,你只需CTRL + A, CTRL + C , CTRL +V):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC
"-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN"
"http://getahead.org/dwr/dwr20.dtd">
<dwr>
<allow>
<create javascript="RemotePojo" creator="new">
<param name="class" value="com.brofe.dwr.pojo.RemotePojo" />
<include method="remoteGetStr"/>
</create>
</allow>
</dwr>
6、新建JSP页面引入如下脚本:
<!-- 导入DWR2.0.3核心的JS文件 -->
<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/engine.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/util.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/interface/RemotePojo.js"></script>
7、调用
<script type="text/javascript">
var show = document.getElementById("show");
function getStr () {
RemotePojo.remoteGetStr(getStrCallback);
}
function getStrCallback (result) {
show.innerText = result;
}
</script>
8、完工
1、下载DWR开发发包,建议下载ZIP的压缩包,方便程序员学习。我使用的DWR版本是:dwr-2.0.3-src
2、新建web工程,在WEB-INF/lib下引入dwr.jar包, 添加dwr.xml文件(在DWR ZIP中有类似的dwr.xml文件可以COPY去掉没有用的内容即可)
3、在web.xml中加入如下配置:
<!-- 配置DWR -->
<servlet>
<display-name>DWR2.0.3 SERVELET</display-name>
<servlet-name>dwr-invoke</servlet-name>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
<init-param>
<description>打开调试模式,程序发布时必须关闭</description>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoke</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
4、写一个简单的JavaBean,如下:
package com.brofe.dwr.pojo;
public class RemotePojo {
public RemotePojo() {}
public String remoteGetStr () {
return "你好,brofe。 欢迎!!!";
}
}
5、在dwr.xml文件添加如下配置(一下是完整的xml文件,你只需CTRL + A, CTRL + C , CTRL +V):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC
"-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN"
"http://getahead.org/dwr/dwr20.dtd">
<dwr>
<allow>
<create javascript="RemotePojo" creator="new">
<param name="class" value="com.brofe.dwr.pojo.RemotePojo" />
<include method="remoteGetStr"/>
</create>
</allow>
</dwr>
6、新建JSP页面引入如下脚本:
<!-- 导入DWR2.0.3核心的JS文件 -->
<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/engine.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/util.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/interface/RemotePojo.js"></script>
7、调用
<script type="text/javascript">
var show = document.getElementById("show");
function getStr () {
RemotePojo.remoteGetStr(getStrCallback);
}
function getStrCallback (result) {
show.innerText = result;
}
</script>
8、完工


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



