I recently got involved in a project that used GWT and Spring. One of the first things that struck me was the lack of support for Dependency injection frameworks in GWT. So we had to come up with a Spring controller that looked similar to this
Here, what we have done is create a spring controller that is also a GWT remote servlet. Only thing to note is that the GWT remote servlet was never intented to serve as a controller. Also we will force spring to accept the response provided by the servlet by returning null from the method. So we are in essence faking the servlet container in the spring controller here. I could have just copied the contents of the RemoteServiceServlet. But that would be duplicating the implementation of the GWT code.
public class SpringAwareGWTService extends RemoteServiceServlet implements Controller {
private ThreadLocalservletContext = new ThreadLocal ();
public final ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) {
servletContext.set(request.getSession().getServletContext());
doPost(request, response);
servletContext.set(null);
return null;
}
public ServletContext getServletContext() {
return servletContext.get();
}
}
Here, what we have done is create a spring controller that is also a GWT remote servlet. Only thing to note is that the GWT remote servlet was never intented to serve as a controller. Also we will force spring to accept the response provided by the servlet by returning null from the method. So we are in essence faking the servlet container in the spring controller here. I could have just copied the contents of the RemoteServiceServlet. But that would be duplicating the implementation of the GWT code.
That is some sweet stuff--looks like exactly what I need to merge our worlds. Thanks for taking the time to post!
ReplyDelete