gxt、gwt与spring结合使用

本文介绍了一种GXT与Spring框架相结合的方法,通过自定义继承RemoteServiceServlet的类实现服务端逻辑,并利用Spring管理业务对象。具体步骤包括创建项目、实现服务端逻辑、配置web.xml等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

一、建立项目工程
建立java project,这个比较简单,大家都会,在此略过。
二、实现RemoteServiceServlet,复写其的相关方法
MyGWTServer 代码:
[quote]
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("[color=red].gwt[/color]"));
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);
}
}
[/quote]

MyGWTServer继承RemoteServiceServlet,做了三件事
1.init时获得spring web context
2.service时解析请求字符,如/service/calculatorService,获得calculatorService名,对应spring bean id
3.重写processCall方法,通过calculatorService,在springContext中获得注册了的calculatorService,提供调用
4.对应的web.xml配置:
[quote]
<?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>[color=red]*.gwt[/color]</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>Caculator.html</welcome-file>
</welcome-file-list>
</web-app>
[/quote]

MyGWTServerTest java代码:
[quote]
public class MyGWTServerTest extends RemoteServiceServlet {

}
[/quote]
MyGWTServerTest详细看附件中的源码。
MyGWTServerTest与MyGWTServer只有其中的init实现上不同,MyGWTServer是从web context 获取 spring的WebApplicationContext,MyGWTServerTest是直接读取applicationContext.xml文件初始化spring bean。
MyGWTServerTest主要用于开发阶段,用于java调试。如*.gwt.xml的配置:
[quote]
<?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[color=red].gwt[/color]"/>
<servlet class="org.sundoctor.caculator.server.MyGWTServerTest" path="/others[color=red].gwt[/color]"/>
</module>
[/quote]

三、业务类实现
业务接口代码:
[quote]
@RemoteServiceRelativePath("calculatorService[color=red].gwt[/color]")
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[color=red].gwt[/color]";
}
return moduleRelativeURL;
}
}
}
[/quote]
在CalculatorService中注意[color=red].gwt[/color]其与第二步中相同的红色地方。

业务实现类代码:
[quote]
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;
}

}
[/quote]
在CalculatorServiceImpl 中不用实现任务GWT类或接口,可以不依赖于GWT。
applicationContext.xml配置:
[quote]
<?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>
[/quote]

四、工程源码:
查看附件。源码中没包括任何相关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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值