首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> J2EE

Spring + DWR Comet 實現無刷新多人聊天室(二):專案搭建、web.xml 與基礎配置全記錄

     編輯:J2EE

依賴準備

  • DWR 3.x:官方構建包;
  • Spring 3.0.5:SpringSource EBR;
  • 所需 JAR (參考截圖):spring-core, context, web, beans, aop, tx, expression, asm, commons-logging, dwr 等。

依賴 JAR 列表

Web 專案創建:DWRComet

web.xml 完整配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="https://java.sun.com/xml/ns/javaee"
         xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="https://java.sun.com/xml/ns/javaee
                             https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- Spring 容器啟動 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:applicationContext-*.xml</param-value>
    </context-param>

    <!-- DWR 核心 -->
    <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>
        <!-- 關鍵:啟用 Comet (Reverse Ajax) -->
        <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>

</web-app>

關鍵配置解讀

元素作用
ContextLoaderListener啟動 Spring 根容器,載入 applicationContext-*.xml
DwrListener初始化 DWR 運行時、掃描 dwr.xml、註冊創建器
DwrServlet處理 /dwr/* 請求:JS 腳本生成、RPC 調用、Comet 長連接
pollAndCometEnabled=true必須開啟,否則 Reverse Ajax 無法工作
debug=true開發期開啟,生產建議關閉

Spring Bean 配置 applicationContext-beans.xml (初始骨架)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
       xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="https://www.springframework.org/schema/aop"
       xmlns:tx="https://www.springframework.org/schema/tx"
       xmlns:util="https://www.springframework.org/schema/util"
       xmlns:context="https://www.springframework.org/schema/context"
       xsi:schemaLocation="
           https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           https://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           https://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx-3.0.xsd
           https://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util-3.0.xsd
           https://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <!-- 具體 Bean 於後續篇幅註冊 -->
</beans>

DWR 配置 dwr.xml (最小骨架)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN"
        "https://getahead.org/dwr/dwr30.dtd">
<dwr>
    <!-- 具體 allow/create/convert 於後續篇幅配置 -->
</dwr>

開發環境記錄

  • OS:Windows;
  • 瀏覽器:IE6+ / Firefox 3+;
  • 容器:Tomcat 5.5 / 6.0 (生產建議 Jetty 以支援 Continuation);
  • IDE:Eclipse / MyEclipse 8;
  • 規範:Java EE 5、Spring 3.0.5、DWR 3。

基礎設施就緒,下一篇進入業務邏輯實現:Message 實體、ChatMessageEvent、ChatService。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved