DWR(一):搭建
1.下载Dwr
网址:http://directwebremoting.org/dwr/downloads/index.html
2.创建工程、导入JAR
我下载的是dwrdemo.war。解压后:
从lib文件中获得dwr.jar;放入自己web工程;
3.配置web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>testDwr</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!-- Dwr核心Servlet --> <servlet> <servlet-name>dwrServlet</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> <!-- Dwr核心Servlet Url映射--> <servlet-mapping> <servlet-name>dwrServlet</servlet-name> <url-pattern>/dwrServletUrl/*</url-pattern> </servlet-mapping> </web-app>
说明:发往/dwrServletUrl/*的所有请求都有org.directwebremoting.servlet.DwrServlet来处理;
问题:当配好完毕后,启动工程。工程启动不起来了! 报错如下
原因:缺少Jar包。java.lang.NoClassDefFoundError: Lorg/apache/commons/logging/Log; 缺少logging.jar
解决:添加 logging.jar
4.配置Dwr.xml
Dwr.xml文件的作用就是将Java Class 与JavaScript 对应;
首先新建Java类
package com.devan.test
public class Test { String name ; String pass; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPass() { return pass; } public void setPass(String pass) { this.pass = pass; } }
配置Dwr.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN" "http://getahead.org/dwr/dwr30.dtd"> <dwr> <allow> <create creator="new" javascript="MyTest"> <param name="class" value="com.devan.test.Test"/> </create> </allow> </dwr>
访问 : http://localhost:8080/testDwr/dwrServletUrl/