我的集成参考了以下几篇文章。
Bram Smeets的Your first cup of DWR… http://bram.jteam.nl/?p=2

Dejan Bosanac的Spring and DWR revisited
http://www.oreillynet.com/onjava/blog/2005/12/spring_and_dwr_revisited.html

邢红瑞的dwr与spring相结合 http://blogger.org.cn/blog/more.asp?name=hongrui&id=9920


1,应用的结构:
 1dwr
 2dwr/src/dwr.manager/UserManager.java
 3dwr/src/applicationContext.xml
 4dwr/src/log4j.properties
 5dwr/WEB-INF
 6dwr/WEB-INF/web.xml
 7dwr/WEB-INF/dwr.xml
 8dwr/WEB-INF/springMVC-servlet.xml
 9dwr/WEB-INF/lib #####dwr.jar bsf.jar为必须 bsf.jar 将java bean 转换成javascript
10/dwr/index.jsp



2,spring applicationContext配置
1)applicationContext.xml中只有一个bean

1<?xml version="1.0" encoding="UTF-8" ?>
2<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
3<beans>
4<bean id="UserManager" class="dwr.manager.UserManager"></bean>
5</beans>
6



(一)与Spring MVC集成:
2)spring mvc 配置:
 1<?xml version="1.0" encoding="UTF-8" ?>
 2<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
 3
 4<beans>
 5
 6    <bean id="htmlViewResolver"
 7        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
 8        lazy-init="true">
 9        <property name="prefix" value="/manage/" />
10        <property name="suffix" value=".html" />
11        <property name="order">
12            <value>10</value>
13        </property>
14    </bean>
15<!--
16    <bean id="urlMapping"
17        class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"
18        lazy-init="true">
19
20        <property name="lazyInitHandlers" value="true" />
21
22        <property name="mappings">
23            <props>
24                <prop key="/**/*.js">dwrController</prop>
25                <prop key="exec/*">dwrController</prop>
26        ####         ohter controller
27            </props>
28        </property>
29    </bean>
30    -->
31    <bean id="dwrUrlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
32     <property name="alwaysUseFullPath" value="true"/>
33     <property name="mappings">
34       <props>
35         <prop key="/dwr/**/*">dwrController</prop>
36       </props>
37    </property>
38   </bean>
39    
40  <bean id="dwrController" class= "org.springframework.web.servlet.mvc.ServletWrappingController" lazy-init="true">
41    <property name="servletClass">
42      <value>uk.ltd.getahead.dwr.DWRServlet</value>
43    </property>
44    <property name="initParameters">
45      <props>
46        <prop key="debug">true</prop>
47      </props>
48    </property>
49</bean>
50</beans>


mvc中的两个SimpleUrlHandlerMapping的配置都是能够可以让dwr和spring协同工作的。
关键的区别在于<property name="alwaysUseFullPath" value="true"/>


3)dwr.xml的配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN" "http://www.getahead.ltd.uk/dwr/dwr10.dtd">
<dwr>
    
<allow>  
         
<create creator="spring"           javascript="UserManager">
          
<param name="beanName" value="UserManager"/>
        
</create>
      
</allow>
</dwr>



4)web.xml的配置:
初始化applicationContext片断:

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>




spring mvc配置:
<!-- spring mvc -->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>*.aspx</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>




(二)不与spring mvc整合,仅与spring applicationContext结合:

将所有spring mvc里面关于dwr的配置都删除。在web.xml里添加如下配置:
<servlet>
<servlet-name>dwr</servlet-name>
<servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
</servlet>


<servlet-mapping>
<servlet-name>dwr</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>



整合结束。


index.jsp的代码

<%@ page contentType="text/html;charset=GBK" language="java" %>
<html>
<head><title>Simple jsp page</title></head>
<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/interface/UserManager.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">
    
function showResult(b) {
        
if (b) {
            alert(
"已经有重复的登录名了!");
        }
 else {
            alert(
"登录名尚未注册过!");
        }

    }
function checkRepeat(s) {
        
var o = getElement(s);
        
if (o) {
            UserManager.getUser(o.value, showResult);
        }

    }

    
function getElement(name) {
        
var result = null;
        
var objs = document.getElementsByName(name);
        
if (objs) {
            
var o = objs[0];
            result 
= o;
        }

        
return result;
    }

</script>
<body>
<input type="text" name="user" >
<input type="button" onclick="checkRepeat('user')" value="检查是否重复">
</body>
</html>

posted on 2006-11-21 12:52 fantasyginge 阅读(1118) 评论(1)  编辑  收藏
Comments

只有注册用户登录后才能发表评论。


网站导航: