DWR(Direct WebRemoting)是一个开源的类库,可以帮助开发人员开发包含AJAX技术的网站.它可以允许在浏览器里的代码使用运行在WEB服务器上的JAVA函数,就像它就在浏览器里一样.
一、dwr入门demo
开始之前请在myeclipse下建立web工程dwrWeb
- 下载jar包,放到你的java web应用的WEB-INF/lib 目录下.
注意:从dwr2.0开始,需要用到commons-logging包
- 编辑web.xml,加入servlet,让web应用支持 dwr
<servlet> <display-name>DWRServlet</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> </servlet> <servlet-mapping> <servlet-name>dwr-invoker</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping>
- 在web.xml的同级目录下建立dwr.xml文件,这个配置文件定义了dwr可以创建和调用哪些java类
<?xmlversion="1.0" encoding="UTF-8"?> <!DOCTYPEdwr PUBLIC "-//GetAhead Limited//DTD Direct WebRemoting 2.0//EN" "http://getahead.org/dwr/dwr20.dtd"> <dwr> <allow> <create creator="new"javascript="Demo"> <param name="class"value="com.Demo"/> </create> </allow> </dwr>
- 建立java类文件
package com; publicfinal class Demo { public String hello(String name) { return "恭喜恭喜," + name +"你的dwr程序成功运行!Hava fun!"; } }
- 建立js文件
functionhello() { var name =dwr.util.getValue("name"); Demo.hello(name, function(data) { dwr.util.setValue("result",data); });
- 建立index.jsp文件
<!DOCTYPEHTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="Content-Type"content="text/html; charset=utf-8" /> <script type='text/javascript'src='<%=request.getContextPath()%>/dwr/interface/Demo.js'> </script> <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='hello.js'> </script> </head> <body> <input type="text"name="name" value="Hello World!"> <br> <input type="button"onclick="hello();" value="button"> <br> <span id="result"style="background: #eeffdd; padding: 10px"></span> </body> </html>
二、接下来为dwr+spring集成步骤:
- 新建一个web工程,导入dwr+spring所需jar,如下图
- 在web.xml配置文件中添加spring的配置。以及dwr的配置;
<?xmlversion="1.0" encoding="UTF-8"?> <web-appversion="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <!--Spring上下文 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:resource/app*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置DWR前端控制器 --> <servlet> <servlet-name>dwrServlet</servlet-name> <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class> <!-- 指定配置文件 --> <init-param> <param-name>config</param-name> <!-- 如果有多个用","分开 --> <param-value> /WEB-INF/classes/config/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>dwrServlet</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping> </web-app>
- 配置applicationContext.xml
<?xmlversion="1.0" encoding="UTF-8"?> <!-- 配置系统基础环境 --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" > <bean id="demo" class="com.Demo"></bean> </beans>
- 配置dwr.xml
<?xmlversion="1.0" encoding="UTF-8"?> <!DOCTYPEdwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN""http://getahead.org/dwr/dwr20.dtd"> <!--通用dwr配置 --> <dwr> <allow> <!-- 建立JS对象,将目标对象的方法转换成JS对象的方法--> <createcreator="spring"javascript="Demo"> <param name="beanName" value="demo"/> </create> </allow> </dwr>
dwr.xml的配置是整个与spring结合的关键。通过简单的学习,我想你也可以轻松的上手dwr了。
时间: 2024-10-07 17:36:06