Springfying GWT
Google toolkit allows usage of Spring framework in the server side. The
goal of this approach is to decouple the business interface (defined in the
client side) from the web interface.
Step 1:
Create business interface created in the client package that extends
RemoteService interface
package demo.client;
public interface Test extends RemoteService {
public static final String SERVICE_URI = "RPCHandler";
public static class Util {
public static TestAsync getInstance() {
TestAsync instance = (TestAsync) GWT.create(Test.class);
ServiceDefTarget target = (ServiceDefTarget) instance;
target.setServiceEntryPoint(GWT.getModuleBaseURL() +
SERVICE_URI);
return instance;
}
}
public String sayHello(String name);
}
Step 2:
Create the asynchronous business interface (e.g. MyServiceAsycn.java).
(Note: The <interfaceName>Async naming convention is important for the GWT compiler)
package demo.client;
public interface TestAsync {
public void sayHello(String s, AsyncCallback<String> callback);
}
Step 3:
The dispatcher Servlet acts as the controller to invoke the services implementations that are defined as spring beans. The dispatcher Servlet uses
WebApplicationContextUtils.getWebApplicationContext(getServletContext()).getBean(interfaceName)
to get the requested service implementation bean. The dispatcher Servlet design follows these steps:
Create an ordinary Servlet to handle all GWT RPC calls, and then Servlet
implementation is responsible for invoking the requested service
method.
Define the service implementation as a spring bean to implement the
business interface defined at the client.
package demo.server;
@Transactional
public class HandlerService implements Test {
public String sayHello(String name) {
return "Hello " + name;
}
Use RPC.decodeRequest utility class to get the RPC request, which contains
the business interface name , method to be invoked and parameters, the
encoded request looks similar to this pattern:
http://host:8888/TestValidation/?485975FDB961BEAB5B5D6F881FE9205A?demo.client.MyService?saveUser
?demo.client.UserDto?demo.client.UserDto/1050776168?text?1?2?3?4?1?5?6?2342?7?
where,
demo.client.MyService: Interface name
saveUser: Method to be invoked
demo.client.UserDto: method parameters object type(s)
Service implementation invocation design approaches:
- The service implementation is defined as a Spring bean , with an ID
equals to the name of the interface - The service implementation is defined as a Spring bean with id equals
to the name of the interface and method (<interface
FQN>.methodname;
The RPC Handler Servlet ,invokes the method requested in the payload on
the retrieved spring bean
The RPC Handler uses the utility classes that comes with the GWT library
(RPC , RPCServletUtil …)
protected void doPost(HttpServletRequest httpRequest,HttpServletResponse httpResponse) throws ServletException,IOException {
String payload =
RPCServletUtils.readContentAsUtf8(httpRequest);
Object service = null;
String responsePayload = null;
RPCRequest rpcRequest = RPC.decodeRequest(payload);
Class clazz = rpcRequest.getMethod().getDeclaringClass();
service = getService(clazz.getName());
responsePayload = RPC.invokeAndEncodeResponse(service, rpcRequest.getMethod(), rpcRequest.getParameters(), rpcRequest.getSerializationPolicy());
boolean gzipEncode = false;
RPCServletUtils.writeResponse(getServletContext(),httpResponse,responsePayload, gzipEncode);
No comments yet.
Leave a comment
| Next »
-
Recent
-
Links
-
Archives
- July 2009 (1)
- October 2008 (3)
-
Categories
-
RSS
Entries RSS
Comments RSS