2007-11-25
gwt spring 完美整合
关键字: gwt spring1.3版本前整合gwt spring,用到了第三方类,如cglib扩展类具备gwt servlet的功能,随着1.4版本的发布(发布很久了T_T '),整合有了新的方式,闲话不说,看看整合后的servlet如何调用服务:
java 代码
- /**
- * GWTRemoteServiceServlet act as a dispatch servlet for all GWT services
- *
- * in your web.xml, mapping all request /gwtrpc/ to this servlet
- *
- */
- public class GWTRemoteServiceServlet extends RemoteServiceServlet {
- 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 void service(HttpServletRequest req, HttpServletResponse resp)
- throws ServletException, IOException {
- try {
- HttpRequestContext context = new HttpRequestContext(req, resp, this
- .getServletConfig());
- HttpRequestContext.ThreadLocalHttpRequestContext.set(context);
- ServletContext servletContext = getServletContext();
- perThreadRequest.set(req);
- perThreadResponse.set(resp);
- String pathInfo = req.getPathInfo();
- if (pathInfo.startsWith("/")) {
- pathInfo = pathInfo.substring(1);
- }
- RemoteService service = (RemoteService) springContext.getBean(pathInfo);
- 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();
- }
- }
- /**
- * 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);
- }
- }
- }
xml代码:
- <bean id="bookservice" class="com.service.BookService" />
java 代码
- class BookService implements IBookService{
- }
- interface IBookService extends RemoteService{
- }
ok,该servlet继承RemoteServiceServlet,做了三件事
1.init时获得spring webContext
2.service时解析请求字符,如/service/bookservice,获得bookservice名,对应spring bean id
3.重写processCall方法,通过bookservice,在springContext中获得注册了的bookservice,提供调用
评论
yongyuan.jiang
2008-06-14
gwt前端定义一个异常类
public class ApplicationException extends SerializableException implements
IsSerializable {
后台出错抛出这个异常即可
public class ApplicationException extends SerializableException implements
IsSerializable {
后台出错抛出这个异常即可
angeltping
2008-06-04
(gwt+spring)我想用aop拦截来判断session超时,但是我怎样才能能像客户端抛异常了让客户端知道我这个掉方法失败的exception是因为session超时了
abo
2008-05-14
博主是javaeye上的gwt第一高人,特别希望博主可以就如何用gwt开发一个伸展性比较好的应用整理出一篇完整的文章,这样大家学习起来也比较方便。
再次谢谢博主。
再次谢谢博主。
yongyuan.jiang
2008-01-09
哦,是这样子的。
1 perThreadRequest和perThreadResponse是private的,你这里怎么可以引用?
我把父类的方法全部写成protected了
2 HttpRequestContext这个类是哪里的?
这个类是自己写的一个类,用于存放requestde
3 readPayloadAsUtf8这个方法为什么我引用不到?
这个方法也是在父类当中
1 perThreadRequest和perThreadResponse是private的,你这里怎么可以引用?
我把父类的方法全部写成protected了
2 HttpRequestContext这个类是哪里的?
这个类是自己写的一个类,用于存放requestde
3 readPayloadAsUtf8这个方法为什么我引用不到?
这个方法也是在父类当中
cloudyofsky
2008-01-02
yongyuan.jiang,对你的代码我有些疑惑,还请多多指教。
1 perThreadRequest和perThreadResponse是private的,你这里怎么可以引用?
2 HttpRequestContext这个类是哪里的?
3 readPayloadAsUtf8这个方法为什么我引用不到?
谢谢!
1 perThreadRequest和perThreadResponse是private的,你这里怎么可以引用?
2 HttpRequestContext这个类是哪里的?
3 readPayloadAsUtf8这个方法为什么我引用不到?
谢谢!
yongyuan.jiang
2007-12-03
so good ,唉。我对spring那个ModelAndView 还是不懂
gwbasic
2007-11-30
谢谢yongyuan.jiang,支持aop
另一个版本
delegate 用法参照 org.springframework.web.servlet.mvc.multiaction.MultiActionController
另一个版本
delegate 用法参照 org.springframework.web.servlet.mvc.multiaction.MultiActionController
public class GwtRemoteServiceController extends RemoteServiceServlet implements
Controller, ServletContextAware {
private static final long serialVersionUID = 8175888785480720736L;
private Object delegate;
private ServletContext servletContext;
@Override
public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext;
}
@Override
public ServletContext getServletContext() {
return servletContext;
}
@Override
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
super.doPost(request, response);
return null;
}
@Override
public String processCall(String payload) throws SerializationException {
Object delegateToUse = this.delegate;
if (delegateToUse == null) {
return super.processCall(payload);
} else {
try {
RPCRequest rpcRequest = RPC.decodeRequest(payload,
delegateToUse.getClass(), this);
return RPC.invokeAndEncodeResponse(delegateToUse, 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);
}
}
}
public Object getDelegate() {
return delegate;
}
public void setDelegate(Object delegate) {
this.delegate = delegate;
}
}
yongyuan.jiang
2007-11-28
方便快速强大,不要把简单问题复杂化
yongyuan.jiang
2007-11-28
支持啊,其实就是gwt调用了一个spring服务,而spring服务是一个gwt服务而已
gwbasic
2007-11-27
这样做好像不支持aop
yongyuan.jiang
2007-11-26
新架构优势:
1.没有使用第三方包扩展,更快速、更稳定
2.由一个servlet转发请求,服务类为spring注册的类,无需继承RemoteServlet,服务类不是servlet
1.没有使用第三方包扩展,更快速、更稳定
2.由一个servlet转发请求,服务类为spring注册的类,无需继承RemoteServlet,服务类不是servlet
发表评论
提醒: 该博客已发表在公共论坛,博客所有留言会成为论坛回贴,留言请注意遵守论坛发贴规则
- 浏览: 10757 次
- 性别:

- 来自: 广州

- 详细资料
搜索本博客
最近加入圈子
最新评论
-
gwt spring 完美整合
gwt前端定义一个异常类 public class ApplicationExc ...
-- by yongyuan.jiang -
gwt spring 完美整合
(gwt+spring)我想用aop拦截来判断session超时,但是我怎样才能 ...
-- by angeltping -
GWT HTML Template :模板 ...
hehe,HTMLTemplatePanel增强功能,gwt类直接获得模板页面对 ...
-- by yongyuan.jiang -
Gwt 服务端使用hiberante ...
能说说都有哪些问题?为什么产生?如何解决? 正打算在一个GWT项目的服务器端使用 ...
-- by abo -
gwt spring 完美整合
博主是javaeye上的gwt第一高人,特别希望博主可以就如何用gwt开发一个伸 ...
-- by abo






评论排行榜