简化ajax的使用之DWR学习——与spring的整合

本文详细介绍如何将DWR与Spring框架进行整合。通过创建DwrService类并配置相关XML文件,实现JavaScript直接调用服务端方法。文章还介绍了如何在Spring配置文件中配置DWR远程接口。
[size=medium]Spring与DWR的整合参考:[/size]
[url][size=medium]http://directwebremoting.org/dwr/documentation/server/integration/spring.html#namespace[/size][/url]

[size=medium]学习了DWR之后,感觉倍好用,就继续看了与spring的整合。这个跟之前的一篇[url]http://yimengzhu.iteye.com/blog/1883179[/url]有一些关联,但是我还是重新创建了web 项目:

第一: 利用myeclipse创建web项目,并具有如下创建类DwrService,如下结构所示:[/size]
[img]http://dl2.iteye.com/upload/attachment/0085/5023/b0c69738-1dad-3625-bc62-a4d70390a47e.png[/img]
[size=medium]DwrService.java的源码如下:[/size]
package org.piedra.dwr;

public class DwrService {

public DwrService(){
System.out.println("DwrService was inited.");
}

public String getService(int type){
String serviceName = "";
switch(type){
case 1:
serviceName = "springService";
break;
case 2:
serviceName = "dwr&springService";
break;
default:
serviceName = "dwrService";
break;
}
return serviceName;
}
}


[size=medium]好吧,我们的服务已经创建完成了。这下需要引入我们spring和dwr所需要的jar包,你自己导着,看缺少哪几个jar包就加入。当然,如果你的项目已经集成了spring,那是最好不过的了。
jar包引入完成后,我们来配置web.xml。在web.xml中我们需要配置spring配置文件的位置,监听器。[/size]
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<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>
</web-app>

[size=medium]好吧,根据我们web.xml的配置,我们要在WEB-INF目录下创建applicationContext.xml。我配置好了,如下:[/size]
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">

<bean id="dwrService" class="org.piedra.dwr.DwrService">
</bean>
</beans>


[size=medium]至此我们的spring已经配置完成了。
接下来是配置dwr了。关于DWR的配置我们在之前的那篇博客已经有介绍过了[url]http://yimengzhu.iteye.com/blog/1883179[/url],但是根据DWR官方文档的建议,我们与Spring集成的时候强烈不推荐使用dwr.xml配置文件。而是在spring配置文件中直接配置我们的远程调用接口。要在spring的配置文件中配置dwr首先需要加入dwr的名称空间:[/size]
xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"

[size=medium]和[/size]

http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd

[size=medium]然后还需要配置我们需要暴露出去的接口,根据我们的例子,我们需要将我们的DwrService暴露给浏览器的javascript直接访问,那么,我么在bean节点下面创建使用子节点<dwr:remote>其中 javascript指定我们在浏览器需要使用的对象名称。
结果,spring的配置文件编程下面这样:[/size]
<?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:context="http://www.springframework.org/schema/context"
[color=red]xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"[/color]
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
[color=red]http://www.directwebremoting.org/schema/spring-dwr
http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd[/color]">

<bean id="dwrService" class="org.piedra.dwr.DwrService">
<dwr:remote javascript="springDwrService">
</dwr:remote>
</bean>
</beans>

[size=medium]配置到这里,我们已经将dwr跟spring整合到一起了,但是我们要使得我们的javascript能够使用这个对象还需要在web.xml配置dwr的servlet。这个配置跟之前那篇配置基本一样,只不过换了servlet的实现类。从org.directwebremoting.servlet.DwrServlet换成了org.directwebremoting.spring.DwrSpringServlet
在web.xml配置玩dwr之后,web.xml配置文件变成了:[/size]
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<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</servlet-name>
<servlet-class>org.directwebremoting.spring.DwrSpringServlet</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>

</web-app>


[size=medium]到此为止我们的配置已然全部结束了。好吧,就来验证下是否成功集成dwr和spring
在浏览器中输入: http://localhost:8080/dwrSpring/dwr 有如下效果:[/size]
[img]http://dl2.iteye.com/upload/attachment/0085/5079/7b61a29a-b95c-3a9a-af0b-b17a03f7d544.png[/img]
[size=medium]
然后我们点击springDwrService进入另一个详细页面。[/size]
[img]http://dl2.iteye.com/upload/attachment/0085/5077/1f7af7e7-5dfc-3e47-8652-429aa1c7c045.png[/img]

[size=medium]看到上图页面也就代表我们的服务能够成功的在javascript中调用了。具体的调用方法还是跟上一篇博客[url]http://yimengzhu.iteye.com/blog/1883179[/url]的index.jsp页面中的调用方式一样。

继续学习中、、、

谢谢阅读!
[/size]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值