Enterprise Java
Spring GWT Integration using the RequestFactory API
Beginning from GWT 2.4 the integration of the RequestFactory API with Spring services on the backend is easy all you need to do is create a custom ServiceLocator on your server which will be used by GWT to locate properly the called services :
public class SpringServiceLocator implements ServiceLocator { public Object getInstance(Class clazz) { ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext( RequestFactoryServlet.getThreadLocalServletContext()); return context.getBean(clazz); } }
The second step is to declare you RequestFactory servlet on your web.xml like this, (I assume that you have already spring set up) :
<servlet> <servlet-name>requestFactoryServlet</servlet-name> <servlet-class>org.gxpenses.util.SpringRequestServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>requestFactoryServlet</servlet-name> <url-pattern>/gwtRequest</url-pattern> </servlet-mapping>
As usual on the GWT part you have to configure your Proxies (because you use service style backend, you have to use ValueProxy instead of the EntityProxy) and your Requests which are the remote interfaces of your services :
//Note that I inherit from the ValueProxy object @ProxyFor(Account.class) public interface AccountProxy extends ValueProxy { public String getId(); public void setId(String id); public String getName(); public void setName(String name); public Double getBalance(); public void setBalance(Double balance); public String getType(); public void setType(String type); } //You have to provide you service Impl class, and the ServiceLocator you created //Note that Account is automatically to AccountProxy on the client @Service(value=AccountsServiceImpl.class, locator=SpringServiceLocator.class) public interface AccountRequest extends RequestContext { abstract Request<Void> createNewAccount(AccountProxy account); abstract Request<Void> updateAccountBalance(String accountId, Double transactionAmount, String type); abstract Request<Double> totalAmountByAccountAndPeriodeAndType(String accountId, Date start, Date end, String type); }
That’s it for the integration, for more informations on how to use the RequestFactory API see : RequestFactory API
Reference: Spring GWT Integration using the RequestFactory API from our JCG partner Idriss Mrabti at the Fancy UI blog.
Related Articles :
- GWT 2 Spring 3 JPA 2 Hibernate 3.5 Tutorial – Eclipse and Maven 2 showcase
- GWT 2 Spring 3 JPA 2 Hibernate 3.5 Tutorial
- Building your own GWT Spring Maven Archetype
- Bootstrapping a web application with Spring 3.1 and Java based Configuration, part 1
- GWT Spring and Hibernate enter the world of Data Grids
- Testing GWT Apps with Selenium or WebDriver
- GWT, GWT-Ext (SmartGWT), GXT (Ext GWT) Common Tasks
- GWT EJB3 Maven JBoss 5.1 integration tutorial
- Getting Started with SmartGWT for awesome GWT interfaces