yunshichen

我相信人生是值得活的,尽管人在一生中必须遭受痛苦,卑劣,残酷,不幸和死亡的折磨,我依然深信如此.但我认为人生不一定要有意义,只是对一些人而言,他们可以使人生有意义. ---J 赫胥黎

Spring 2.5 & Dwr 2.0 配置文档


1 Web.xml 配置


    < servlet >
        
< servlet-name > dwr-invoker </ servlet-name >
        
< servlet-class >
            org.directwebremoting.spring.DwrSpringServlet
        
</ servlet-class >
        
< init-param >
            
< param-name > debug </ param-name >
            
< param-value > true </ param-value >
        
</ init-param >
        
< load-on-startup > 3 </ load-on-startup >
    
</ servlet >

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


这里有个小技巧.查看Dwr源码,在DwrSpringServlet中它会查找Spring的WebApplicationContext,如下:
WebApplicationContext webappContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);

container 
= new SpringContainer();
container.setBeanFactory(webappContext);
ContainerUtil.setupDefaults(container, servletConfig);
ContainerUtil.setupFromServletConfig(container, servletConfig);

所以,你必须在Web.xml中加上Spring的ContextLinstener才能正常使用Dwr:
<!--Configurate Spring first!-->
<
context-param>
        
<param-name>contextConfigLocation</param-name>
        
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

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

<servlet>
        
<servlet-name>dwr-invoker</servlet-name>
        
<servlet-class>
            org.directwebremoting.spring.DwrSpringServlet
        
</servlet-class>
        
<init-param>
            
<param-name>debug</param-name>
            
<param-value>true</param-value>
        
</init-param>
        
<load-on-startup>3</load-on-startup>
</servlet>

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

2 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:dwr
="http://www.directwebremoting.org/schema/spring-dwr"
    xsi:schemaLocation
="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.directwebremoting.org/schema/spring-dwr
http://www.directwebremoting.org/schema/spring-dwr-2.0.xsd"
>

    
<!--<dwr:controllerid="dwrController"debug="true"/>-->

    
<dwr:configuration>
        
<dwr:convert
            
class="ww.support.web.dwr.validate.FieldValidateInfo" type="bean">
        
</dwr:convert>
    
</dwr:configuration>

    
<bean id="genericValidatorDwr"
        class
="ww.support.web.dwr.validate.GenericValidator">
        
<dwr:remote javascript="genericValidatorDwr">
            
<dwr:include method="isEmpty" />
            
<dwr:include method="isNotNumeric" />
            
<dwr:include method="isNumberNotInRange" />
            
<dwr:include method="isNumberGreaterThan" />
        
</dwr:remote>
    
</bean>

    
</beans>


3 jsp的调用


<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'src='<%=request.getContextPath()%>/dwr/interface/genericValidatorDwr.js'>
</script>

<script type='text/javascript'>
genericValidatorDwr.isNotNumeric(name,value,message, {
           callback:
function(dataFromServer) {
                   yourFunction(dataFromServer);
           }
           });

function yourFunction(data){
    alert(data.message);
}


</script>





posted on 2008-07-21 10:40 Chenyunshi 阅读(1721) 评论(0)  编辑 收藏 引用 所属分类: Web development

只有注册用户登录后才能发表评论。
<2024年11月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

导航

统计

常用链接

留言簿(7)

随笔分类

随笔档案

文章分类

相册

搜索

最新评论

阅读排行榜

评论排行榜