spring集成gwt的办法

spring集成gwt,没有官方的解决方案,无论spring还是gwt。

在这里使用了第三方的一个集成方法,见:

http://code.google.com/p/spring4gwt/

使用的步骤为,首先,需要将该项目的jar文件放置到WEB-INF/lib目录下。

然后,需要在web.xml文件中设置对spring的支持:

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

<servlet>
    <servlet-name>springGwtRemoteServiceServlet</servlet-name>
    <servlet-class>org.spring4gwt.server.SpringGwtRemoteServiceServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>springGwtRemoteServiceServlet</servlet-name>
    <url-pattern>/standard_console/rpc/*</url-pattern>
</servlet-mapping>

 

其中,listener部分,是加载spring的webapplication context的,SpringGwtRemoteServiceServlet部分,是spring4gwt实现的集成spring和gwt的解决方案。gwt原本是要求写Servlet作为rpc的实现的,然后一个一个的配置到web.xml文件中。有了这个解决方案,具体的rpc的servlet就被spring的bean替代了,这里的*通配所有bean的名字,springGwtRemoteServiceServlet会根据spring的Context获取到具体的实现bean。

那么需要在WEB-INF目录下有一个名为applicationContext.xml的文件,比如:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
     <context:component-scan base-package="com.easymorse.server" />
</beans>

这里设置扫描的package就是你服务器端gwt rpc的包名。

做rpc的类:

package com.easymorse.server;

import org.springframework.stereotype.Service;

import com.easymorse.client.GreetingService;
import com.easymorse.client.bean.BlackUser;

@Service("greet")
public class GreetingServiceImpl implements
        GreetingService {

    @Override
    public String greetServer(BlackUser blackUser) {
        return "中文";
    }
}

 

另外,别忘记在gwt client端的代码中做路径的设置:

package com.easymorse.client;

import com.easymorse.client.bean.BlackUser;
import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;

/**
* The client side stub for the RPC service.
*/
@RemoteServiceRelativePath("rpc/greet")
public interface GreetingService extends RemoteService {
    String greetServer(BlackUser blackUser);
}

 

这里的路径是rpc/greet,其中greet就是bean的名字。

这样,就可以在不影响gwt的各种配置的情况下,实现了简单的基于spring的rpc。

PDF格式打印機    发送文章为PDF   

这篇文章上的评论的 RSS feed TrackBack URI

Leave a Reply