如题, 实现二者的集成的大体思路是:
- 由spring的DispatcherServlet拦截来之GWT client的请求,
- 并委派给spring的bean.
假设, 由服务接口RPCService, 实现类RPCServiceImpl.
- RPCService.java
public interface RPCService extends RemoteService {
// RPCService方法声明...
}
实现方案 1
将RemoteServiceServlet改造为Controller.
- web.xml
<servlet> <servlet-name>gwt-rpc</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>gwt-rpc</servlet-name> <url-pattern>/<package>.<modulename>/gwt-rpc/* </url-pattern> </servlet-mapping>
- gwt-rpc-servlet.xml
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <map> <entry key="/content" value-ref="RPCService" /> </map> </property> </bean> <bean id="RPCService" class="<package>.server. RPCServiceImpl " />
- Controller适配器
/**
* 运用适配器模式,实现RemoteServiceServlet到Controller的适配改造。
* 此外,实现ServletContextAware为RPC的序列化提供servletContext实例
* 。若不实现的话,会在运行时抛出NullPointException
*
* @author zhongl
*
*/
public class GWTController extends RemoteServiceServlet implements
Controller,ServletContextAware{
/* */
private static final long serialVersionUID = 1L;
private ServletContext servletContext;
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
doPost(request, response);
return null;
}
public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext;
}
@Override
public ServletContext getServletContext() {
return servletContext;
}
}
- Service扩展适配器
public class RPCServiceImpl extends GWTController implements RPCService{
// RPCService方法实现...
}
实现方案 2
使用GWTRPCServiceExporter, 参见gwt-sl. 此方案中, RPCServiceImpl完全不用实现RPCService, 好处在于RPCServiceImpl不依赖与GWT.
- web.xml 同上
- gwt-rpc-servlet.xml
<bean id="handlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> <bean id="RPCService" class="<package>.server. RPCServiceImpl " /> <bean name="/content" class="org.gwtwidgets.server.spring.GWTRPCServiceExporter"> <property name="service" ref="RPCService" /> <property name="serviceInterfaces"> <value> <package>.client. RPCService </value> </property> </bean>
参考资料
- Apress - Pro Web 2.0 Application Development with GWT (2008)
- gwt-widgets-server-0.1.5a doc
注意
若使用maven-googlewebtoolkit2-archetype创建项目, 在打包部署之前务必将web.xml中
com.google.gwt.dev.shell.GWTShellServlet的配置删除掉, 它只在HostMode中有效.