一、准備工作
1、 下載dwr的相關jar包
https://Java.Net/downloads/dwr/Development%20Builds/Build%20116/dwr.jar
程序中還需要spring的相關jar包
http://ebr.springsource.com/repository/app/library/version/detail?name=org.springframework.spring&version=3.0.5.RELEASE
需要的jar包如下
2、 建立一個WebProject,名稱DWRComet
在web.XML中添加dwr、spring配置如下:
- <-- 加載Spring容器配置 -->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <-- 設置Spring容器加載配置文件路徑 -->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath*:applicationContext-*.XML</param-value>
- </context-param>
- <listener>
- <listener-class>org.directwebremoting.servlet.DwrListener</listener-class>
- </listener>
- <servlet>
- <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>
- <-- dwr的comet控制 -->
- <init-param>
- <param-name>pollAndCometEnabled</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>
3、 在src目錄加入applicationContext-beans.XML配置,這個配置專門配置bean對象,用來配置需要注入的對象。
- <?XML version="1.0" encoding="UTF-8"?>
- <beans XMLns="http://www.springframework.org/schema/beans"
- XMLns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- XMLns:aop="http://www.springframework.org/schema/aop"
- XMLns:tx="http://www.springframework.org/schema/tx"
- XMLns:util="http://www.springframework.org/schema/util"
- XMLns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
- http://www.springframework.org/schema/util
- http://www.springframework.org/schema/util/spring-util-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd">
- </beans>
4、 在WEB-INF目錄添加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>
- </dwr>
以上的准備基本完畢,下面來完成無刷新聊天室代碼