Spring 2.5 & Dwr 2.0 配置文档

本文介绍了如何在项目中集成Direct Web Remoting (DWR),包括Web.xml配置、Bean配置及JSP页面调用方法。通过示例展示了如何设置DWR以实现客户端与服务器间的远程交互,并提供了验证功能的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

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>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值