`
sundoctor
  • 浏览: 324199 次
  • 性别: 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是可以自动设定的。

相关推荐

Global site tag (gtag.js) - Google Analytics