`
jiagou
  • 浏览: 2530097 次
文章分类
社区版块
存档分类
最新评论

DWR在实际项目中的应用以及在调试过程中遇到的问题

 
阅读更多


背景:
用到DWR主要是做服务器推技术;项目以及架构完成,基于SSH的;因为要做推,所以我还得从新把DWR添加进来。

我的项目应用配置如下:

web.xml (其中的调试的初始参数的含义我都写清楚了,就不进行累赘的描述了)

	<!-- dwr框架配置文件 -->
	<servlet>
		<servlet-name>dwr-invoker</servlet-name>
		<!-- 默认的servlet -->
		<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
		<!-- 调试DWR,发布系统时应将其设为false -->  
		<init-param>
			<description>调试DWR,发布系统时应将其设为false</description>
			<param-name>debug</param-name>
			<param-value>true</param-value>
		</init-param>
		<init-param>
			<description>使用服务器推技术(反转AJAX)</description>
			<param-name>activeReverseAjaxEnabled</param-name>
			<param-value>true</param-value>
		</init-param>
		<init-param>
			<param-name>initApplicationScopeCreatorsAtStartup</param-name>
			<param-value>true</param-value>
		</init-param>
		<init-param>
			<param-name>org.directwebremoting.extend.ServerLoadMonitor</param-name>
			<param-value>org.directwebremoting.impl.PollingServerLoadMonitor</param-value>
		</init-param>
		<init-param>
			<param-name>pollAndCometEnabled</param-name>
			<param-value>true</param-value>
		</init-param>
		<init-param>
			<param-name>maxWaitAfterWrite</param-name>
			<param-value>500</param-value>
		</init-param>
		<load-on-startup>4</load-on-startup>
		<!-- 跨域调用配置信息,不是必须的 -->  
		<!-- 
        <init-param>  
            <param-name>crossDomainSessionSecurity</param-name>  
            <param-value>false</param-value>  
        </init-param>  
         -->
		<!-- 在WEB启动时是否创建范围为application的creator -->  
		<!-- 
		<init-param>
			<description>在WEB启动时是否创建范围为application的creator</description>   
			<param-name>
				initApplicationScopeCreatorsAtStartup
			</param-name>
			<param-value>true</param-value>
		</init-param>
         -->
		
		<!-- DWR默认采用piggyback(客户端请求)方式 -->
		<!-- comet(长连接)方式 -->
		<!-- 在2.0.4之后可以设置长连接的时间通过maxwaitAfterwrite参数进行设置 -->
		<!--
		<init-param>
			<param-name>activeReverseAjaxEnabled</param-name>
			<param-value>true</param-value>
		</init-param>
		<init-param>  
	      <param-name>maxWaitAfterWrite</param-name>  
	      <param-value>500</param-value>  
	    </init-param>  
		-->
		<!-- polling(轮询)方式:在comet方式的基础之上,再配置以下参数 -->
		<!--
		<init-param>
			<param-name>org.directwebremoting.extend.ServerLoadMonitor</param-name>
			<param-value>org.directwebremoting.impl.PollingServerLoadMonitor</param-value>
		</init-param>
		-->
		<!-- 设定轮询求情的时间,参数是毫秒。页面默认的请求间隔时间是5秒 -->
		<!-- 貌似2.0.2之前的版本的disconnecedTime存在错误,需要添加timeToNextPoll参数(未测试) -->
		<!--
		<init-param>
			<param-name>disconnectedTime</param-name>
			<param-value>6000</param-value>
		</init-param>
		<init-param>  
	        <param-name>timeToNextPoll</param-name>  
	        <param-value>6000</param-value>  
	    </init-param>  
		-->
		<!-- 使用polling和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>
dwr.xml

<dwr>
	<allow>
		<create javascript="DWRService" creator="spring" >
			 <param name="beanName" value="DWRService"/>
		</create>
	</allow>
</dwr>

applicaltionContext.xml

先引入命名空间:

xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr" 
       xsi:schemaLocation="
         http://www.directwebremoting.org/schema/spring-dwr 
         http://www.directwebremoting.org/schema/spring-dwr/spring-dwr-3.0.xsd">
再设置做推技术的bean文件

	<!-- 配置dwr -->
	<bean id="DWRService" class="com.wenice.web.dwr.DWRService">
		<dwr:remote javascript="DWRService">
			<dwr:include method="send" />
	    </dwr:remote>  
	</bean>    	 
java代码:

    /** 
     * 处理发送信息相关 
     *  
     * @author 张阳
     *  
     */  
    public class DWRService {
        /** 保存当前在线scriptSession列表 */  
        public static HashMap<String, ScriptSession> scriptSessions = new HashMap<String, ScriptSession>();  
        /**
         * 更新scriptSession列表
         *  
         * @param username 	用户名
         * @param flag		是否存入map中
         * @param request 	请求
         * @return
         */
        public String updateUsersList(String username, boolean flag, HttpServletRequest request) {  
            // 这里取会话(HttpSession)的id为用户id   
        	String httpSessionId = request.getSession().getId();
            if (flag) {   
            	this.setScriptSessionFlag(username);
                //将HttpSessionid和页面脚本session绑定  
                scriptSessions.put(httpSessionId, WebContextFactory.get().getScriptSession());
            }else{  
                return null;  
            }  
            return httpSessionId;  
        }  
      
        /** 
         * 将用户id和页面脚本session绑定 
         * @param userid 
         */  
        public void setScriptSessionFlag(String username) {  
            WebContextFactory.get().getScriptSession().setAttribute("username", username);  
        }  
      
        /** 
         * 根据用户id获得指定用户的页面脚本session 
         * @param userid 
         * @param request 
         * @return 
         */  
        @SuppressWarnings("unchecked")  
        public List<ScriptSession> getScriptSession(String userid, HttpServletRequest request,String server) {  
        	List<ScriptSession> scriptSessionlist = new ArrayList<ScriptSession>();
            ScriptSession scriptSessions = null;  
            java.util.Iterator it = this.scriptSessions.entrySet().iterator();
            while(it.hasNext()){
	            java.util.Map.Entry entry = (java.util.Map.Entry)it.next();
            	ScriptSession session = (ScriptSession) entry.getValue();
            	String xuserid = (String) session.getAttribute("username");
            	if (xuserid != null && xuserid.equals(userid)) {  
            		scriptSessionlist.add(session);
            	}  
            }
            return scriptSessionlist;  
        }  
          
        /** 
         * 发送消息 
         * @param sender 发送者 
         * @param receiverid 接收者id 
         * @param msg 消息内容 
         * @param request 
         */  
        public void send(String sender,String receiverid,String msg,String server,HttpServletRequest request){  
        	List<ScriptSession> sessions = this.getScriptSession(receiverid, request,server);  
        	for (ScriptSession scriptSession : sessions) {
                if(null!=scriptSession){
                    Util util = new Util(scriptSession);  
                    util.setStyle("messageImage", "display", "");  
                }
			}
        }
    }  


java的实现代码其实没有什么重要的;主要就是配置文件的设置。

其中遇到了许多问题;有一些也已经忘记了。都是有问题,就去百度google;

一:大概还记得就是找不到bean这个就是你dwr.xml配置的问题;如果你选用spring进行注入bean;如下:

<create javascript="DWRService" creator="spring" >
			 <param name="beanName" value="DWRService"/>
		</create>
那么param中的name一定要等于:beanName;value是你spring中已经初始化的bean的名称
<bean id="DWRService" class="com.wenice.web.dwr.DWRService">
		<dwr:remote javascript="DWRService">
			<dwr:include method="send" />
	    </dwr:remote>  
	</bean> 
第二个就是ie没有响应的问题。

二:发现进入ie的时候,后台会报failed to find parameter: batchId;这个问题纠结了我两天

今天发现是我配置文件写的有问题。我将之前写的dwr.xml中的

<convert converter="bean" match="com.wenice.domain.user"/>
还有applicationContext.xml中的
<dwr:configuration>
        <dwr:convert type="bean" class="com.wenice.domain.user" />  
    </dwr:configuration>
删除了。

三:还有就是回调函数的问题;

这个问题你可以百度google就可以解决了。上面的两个问题是我找了很多帖子都没解决的。最后重新抽出项目才发现的问题。



下面上传一下我的小测试用例。大家互相学习学习


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics