gwt spring 完美整合

1.3版本前整合gwt spring,用到了第三方类,如cglib扩展类具备gwt servlet的功能,随着1.4版本的发布(发布很久了T_T '),整合有了新的方式,闲话不说,看看整合后的servlet如何调用服务:

java 代码
  1. /**  
  2.  * GWTRemoteServiceServlet act as a dispatch servlet for all GWT services  
  3.  *   
  4.  * in your web.xml, mapping all request /gwtrpc/ to this servlet  
  5.  *   
  6.  */  
  7. public class GWTRemoteServiceServlet extends RemoteServiceServlet {   
  8.   
  9.     private WebApplicationContext springContext;   
  10.   
  11.     @Override  
  12.     public void init(ServletConfig config) throws ServletException {   
  13.            
  14.         super.init(config);   
  15.            
  16.         springContext = (WebApplicationContext) config   
  17.                 .getServletContext()   
  18.                 .getAttribute(   
  19.                         WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);   
  20.   
  21.         if (springContext == null) {   
  22.             throw new RuntimeException(   
  23.                     "Check your web.xml setting, no Spring context configured");   
  24.         }   
  25.   
  26.     }   
  27.   
  28.     @Override  
  29.     protected void service(HttpServletRequest req, HttpServletResponse resp)   
  30.             throws ServletException, IOException {   
  31.         try {   
  32.             HttpRequestContext context = new HttpRequestContext(req, resp, this  
  33.                     .getServletConfig());   
  34.   
  35.             HttpRequestContext.ThreadLocalHttpRequestContext.set(context);   
  36.   
  37.             ServletContext servletContext = getServletContext();   
  38.   
  39.             perThreadRequest.set(req);   
  40.             perThreadResponse.set(resp);   
  41.   
  42.             String pathInfo = req.getPathInfo();   
  43.             if (pathInfo.startsWith("/")) {   
  44.                 pathInfo = pathInfo.substring(1);   
  45.             }   
  46.   
  47.             RemoteService service = (RemoteService) springContext.getBean(pathInfo);   
  48.   
  49.             String requestPayload = readPayloadAsUtf8(req);   
  50.   
  51.             // Let subclasses see the serialized request.   
  52.             //   
  53.             onBeforeRequestDeserialized(requestPayload);   
  54.   
  55.             // Invoke the core dispatching logic, which returns the serialized   
  56.             // result.   
  57.             //   
  58.             String responsePayload = processCall(service, requestPayload);   
  59.   
  60.             // Let subclasses see the serialized response.   
  61.             //   
  62.             onAfterResponseSerialized(responsePayload);   
  63.   
  64.             // Write the response.   
  65.             //   
  66.             writeResponse(req, resp, responsePayload);   
  67.         } catch (Throwable e) {   
  68.             // Give a subclass a chance to either handle the exception or   
  69.             // rethrow it   
  70.             //   
  71.             doUnexpectedFailure(e);   
  72.         } finally {   
  73.             HttpRequestContext.ThreadLocalHttpRequestContext.remove();   
  74.         }   
  75.     }   
  76.   
  77.     /**  
  78.      * rewrite processCall  
  79.      *   
  80.      * @param bean  
  81.      * @param payload  
  82.      * @return  
  83.      * @throws SerializationException  
  84.      */  
  85.     public String processCall(RemoteService bean, String payload)   
  86.             throws SerializationException {   
  87.         try {   
  88.             RPCRequest rpcRequest = RPC.decodeRequest(payload, bean.getClass(),   
  89.                     this);   
  90.             return RPC.invokeAndEncodeResponse(bean, rpcRequest.getMethod(),   
  91.                     rpcRequest.getParameters(), rpcRequest   
  92.                             .getSerializationPolicy());   
  93.         } catch (IncompatibleRemoteServiceException ex) {   
  94.             getServletContext()   
  95.                     .log(   
  96.                             "An IncompatibleRemoteServiceException was thrown while processing this call.",   
  97.                             ex);   
  98.             return RPC.encodeResponseForFailure(null, ex);   
  99.         }   
  100.     }   
  101. }   

 xml代码:

  1. <bean id="bookservice" class="com.service.BookService" />  
java 代码
  1. class BookService implements IBookService{   
  2. }   
  3.   
  4. interface IBookService extends RemoteService{   
  5. }  

ok,该servlet继承RemoteServiceServlet,做了三件事

1.init时获得spring webContext

2.service时解析请求字符,如/service/bookservice,获得bookservice名,对应spring bean id

3.重写processCall方法,通过bookservice,在springContext中获得注册了的bookservice,提供调用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值