`
sundoctor
  • 浏览: 324361 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

gxt、gwt与spring结合使用

    博客分类:
  • gxt
阅读更多
本文参考了yongyuan.jiang列子和网上一些有关文章。
这几天开始研究GXT与Spring结合,不在乎两种方式:
1、使用第三方类库:gwt-widgets-server
2、写一个继承自com.google.gwt.user.server.rpc.RemoteServiceServlet的servlet,结合spring复写其中的一些方法。
第一种方法,我没有试,我觉得第二种方法实现起来更简单,不依赖于任务第三方类库。我实践了第二种方式,把其中的过程式写下来,供大家参考学习,大家如果有什么更好的结合方式,也请多多指教。

一、建立项目工程
建立java project,这个比较简单,大家都会,在此略过。
二、实现RemoteServiceServlet,复写其的相关方法
MyGWTServer 代码:
引用

public class MyGWTServer extends RemoteServiceServlet {
private final ThreadLocal<HttpServletRequest> perThreadRequest = new ThreadLocal<HttpServletRequest>();
private final ThreadLocal<HttpServletResponse> perThreadResponse = new ThreadLocal<HttpServletResponse>();

private WebApplicationContext springContext;

@Override
public void init(ServletConfig Config) throws ServletException {
super.init(Config);
springContext = (WebApplicationContext) Config.getServletContext().getAttribute(
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

if (springContext == null) {
throw new RuntimeException("Check Your Web.Xml Setting, No Spring Context Configured");
}

}

@Override
protected SerializationPolicy doGetSerializationPolicy(HttpServletRequest request, String moduleBaseURL,
String strongName) {
return super.doGetSerializationPolicy((HttpServletRequest) perThreadRequest.get(), moduleBaseURL, strongName);
}

@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {

perThreadRequest.set(req);
perThreadResponse.set(resp);

String requestURI = req.getRequestURI();
String beanname = requestURI.substring(requestURI.lastIndexOf("/") + 1,requestURI.lastIndexOf(".gwt"));
RemoteService service = (RemoteService) springContext.getBean(beanname);

String requestPayload = readPayloadAsUtf8(req);

// Let subclasses see the serialized request.
//   
onBeforeRequestDeserialized(requestPayload);

// Invoke the core dispatching logic, which returns the serialized
// result.
//   
String responsePayload = processCall(service, requestPayload);

// Let subclasses see the serialized response.
//   
onAfterResponseSerialized(responsePayload);

// Write the response.
//   
writeResponse(req, resp, responsePayload);
} catch (Throwable e) {
// Give a subclass a chance to either handle the exception or
// rethrow it
//   
doUnexpectedFailure(e);
} finally {
// HttpRequestContext.ThreadLocalHttpRequestContext.remove();
perThreadRequest.set(null);
perThreadResponse.set(null);
}
}

/**
* rewrite processCall
*
* @param bean
* @param payload
* @return
* @throws SerializationException
*/
public String processCall(RemoteService bean, String payload) throws SerializationException {
try {
RPCRequest rpcRequest = RPC.decodeRequest(payload, bean.getClass(), this);
return RPC.invokeAndEncodeResponse(bean, rpcRequest.getMethod(), rpcRequest.getParameters(), rpcRequest
.getSerializationPolicy());
} catch (IncompatibleRemoteServiceException ex) {
getServletContext().log("An IncompatibleRemoteServiceException was thrown while processing this call.", ex);
return RPC.encodeResponseForFailure(null, ex);
}
}

private String readPayloadAsUtf8(HttpServletRequest request) throws IOException, ServletException {
return RPCServletUtils.readContentAsUtf8(request, true);
}

private void writeResponse(HttpServletRequest request, HttpServletResponse response, String responsePayload)
throws IOException {
boolean gzipEncode = RPCServletUtils.acceptsGzipEncoding(request)
&& shouldCompressResponse(request, response, responsePayload);
RPCServletUtils.writeResponse(getServletContext(), response, responsePayload, gzipEncode);
}
}


MyGWTServer继承RemoteServiceServlet,做了三件事
1.init时获得spring web context
2.service时解析请求字符,如/service/calculatorService,获得calculatorService名,对应spring bean id
3.重写processCall方法,通过calculatorService,在springContext中获得注册了的calculatorService,提供调用
4.对应的web.xml配置:
引用

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>springgxt</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring/*.xml</param-value>
</context-param>
    <listener>      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>      
    </listener>
  
<servlet>
<servlet-name>MyGWTServer</servlet-name> <servlet-class>org.sundoctor.caculator.server.MyGWTServer</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyGWTServer</servlet-name>
<url-pattern>*.gwt</url-pattern>
</servlet-mapping>   
 
  <welcome-file-list>
    <welcome-file>Caculator.html</welcome-file>   
  </welcome-file-list>
</web-app>


MyGWTServerTest java代码:
引用

public class MyGWTServerTest extends RemoteServiceServlet {

}

MyGWTServerTest详细看附件中的源码。
MyGWTServerTest与MyGWTServer只有其中的init实现上不同,MyGWTServer是从web context 获取 spring的WebApplicationContext,MyGWTServerTest是直接读取applicationContext.xml文件初始化spring bean。
MyGWTServerTest主要用于开发阶段,用于java调试。如*.gwt.xml的配置:
引用

<?xml version="1.0" encoding="UTF-8" standalone="no"?><module>
<inherits name="com.google.gwt.user.User"/>
<inherits name="com.extjs.gxt.ui.GXT"/>
<entry-point class="org.sundoctor.caculator.client.Caculator"/>
 
  <inherits name="com.google.gwt.user.theme.standard.Standard"/>
  <servlet class="org.sundoctor.caculator.server.MyGWTServerTest" path="/calculatorService.gwt"/>
  <servlet class="org.sundoctor.caculator.server.MyGWTServerTest" path="/others.gwt"/> 
</module>


三、业务类实现
业务接口代码:
引用

@RemoteServiceRelativePath("calculatorService.gwt")
public interface CalculatorService extends RemoteService {

double add(double a, double b);

double sub(double a, double b);

double mul(double a, double b);

double div(double a, double b);

public static class Util {
private static CalculatorServiceAsync instance;
private static String moduleRelativeURL;

public static CalculatorServiceAsync getInstance() {
if (instance == null) {
instance = GWT.create(CalculatorService.class);
}
return instance;
}

public static String getModuleRelativeURL() {
if (moduleRelativeURL == null || moduleRelativeURL.equals("")) {
moduleRelativeURL = GWT.getModuleBaseURL()+"calculatorService.gwt";
}
return moduleRelativeURL;
}
}
}

在CalculatorService中注意.gwt其与第二步中相同的红色地方。

业务实现类代码:
引用

public class CalculatorServiceImpl  implements CalculatorService {

    public double add(double a, double b) {
return a + b;
    }

    public double div(double a, double b) {
return a / b;
    }

    public double mul(double a, double b) {
return a * b;
    }

    public double sub(double a, double b) {
return a - b;
    }

}

在CalculatorServiceImpl 中不用实现任务GWT类或接口,可以不依赖于GWT。
applicationContext.xml配置:
引用

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="calculatorService" class="org.sundoctor.caculator.server.CalculatorServiceImpl"/>
</beans>


四、工程源码:
查看附件。源码中没包括任何相关jar包,请自己补全:
commons-logging-1.0.4.jar
commons-lang-2.1.jar
gwt-user.jar                 版本1.5.3
gwt-servlet.jar              版本1.5.3
gxt.jar                      版本1.2.3
gxt-servlet.jar              版本1.2.3
spring.jar                   版本2.5.6
6
0
分享到:
评论
2 楼 wiipsp 2010-01-29  
请问如果在serviceImpl端调用 request怎么调用呢   客户端又怎么调用?  用了MyGWTServer后 是不是就只能在MyGWTServer里用request了?  希望楼主能解答···  谢谢·····
1 楼 flashcloud 2009-11-22  
非常感谢你的无私贡献,在仔细阅读和调试了你提供的源代码后,发现一个可以改进的地方,不知是否可以如此,请指正:
CustomerService类你加了GWT的标注@RemoteServiceRelativePath("customerService.gwt")后,在Customer的GWT入口类中,通过initServiceEntryPoint设置入口点路径的代码:
ServiceDefTarget endpoint = (ServiceDefTarget) calculator;		
endpoint.setServiceEntryPoint(CustomerService.Util.getModuleRelativeURL());

这两行代码的操作是多余的,按GWT的AIP参考文档对@RemoteServiceRelativePath的解释是,加上这个标注后,CustomerServiceAsync 的serviceEntryPoint是可以自动设定的。

相关推荐

    搭建简单的EXT-GWT(GXT)的开发环境(三)gxt结合spring和hibernate进行数据操作

    总结起来,EXT-GWT与Spring和Hibernate的集成是企业级Web应用开发的一种常见实践,它利用了GWT的富客户端能力,Spring的服务管理,以及Hibernate的数据库操作便捷性。通过学习和掌握这种集成方式,开发者可以构建出...

    GWT通信机制初探

    总结来说,本文可能会涵盖GWT的通信机制,包括RequestBuilder和AsyncCallback的使用,以及如何将GXT与Spring框架集成,利用Spring的功能如依赖注入、服务代理、安全控制和数据持久化来增强GWT应用的功能和性能。...

    Gxt_BLOG(GXt项目)

    通过"GXt项目",开发者不仅可以学习到如何使用GXT构建高效且美观的Web应用,还能掌握如何将GXT与其他技术如Spring、Hibernate等集成,以实现更全面的业务逻辑。这个项目无疑为初学者和有经验的开发者都提供了一次...

    工作流系统源码,5人Team1年的心血结晶

    技术实现非常适合与研究GWT的人,有GWT的自定义控件、GWT与Spring集成、GWT与Hibernate集成、GWT与FCK集成、Spring与jbpm集成等,总之,技术很多,亮点很多,但恐怕没有深厚的技术功底不能理解...

    webwork+spring+hibernate整合开发 第1讲 part1(浪曦)

    浪曦 的webwork+spring+hibernate

    springbyexample

    将Spring与GXT结合,通常是为了实现后台服务与前端UI的高效交互,例如,使用Spring MVC处理HTTP请求,而GXT负责渲染用户界面和与服务器的数据通信。 在深入学习Spring时,我们通常会涉及以下知识点: 1. **依赖...

    springandgxtsample

    总之,"springandgxtsample" 提供了一个将Spring与GXT结合的实践案例,对于想要了解和掌握这两者集成的开发者来说,是一个宝贵的资源。通过深入研究这个项目,你可以提升在构建复杂、高性能的RIA应用方面的能力。

    hellogwt:使用 Maven、Spring、GWT、Hibernate 和 NetBeans 开发的示例应用程序

    这个项目是一个循序渐进的 Spring-GWT 教程。 它的灵感来自 Alex Tretyakov 在 2012-08-30 上写的文章 。 该项目是使用 GWT cli-tool 创建为“Maven 项目”的。 然后添加、注释和修改来自 hellogwt 的文件以使用 ...

    gwt-boot-starter-mvp4g2:Mvp4g2项目生成器

    笔记: 生成器的实现使用Mvp4g2,GXT,并且基于GWT 2.8.2! 在此版本中,生成的项目需要GWT 2.8.2和Java8。一旦替换了所有对GWT 2.8.2的依赖项,则生成的Mvp4g2源代码将与J2CL / GWT 3一起使用。 您必须选择一个可...

    PagingLoadResult分页

    GXT(Ext GWT)是Sencha公司开发的一套用于构建复杂Web应用的Java库,它扩展了GWT的功能。GXT提供了丰富的UI组件和数据网格分页功能。分页组件通常会集成到数据网格中,允许用户通过分页控件(如页码选择器和每页条...

Global site tag (gtag.js) - Google Analytics