從GWT 2.4開始,將RequestFactory API與后端的Spring服務集成很容易,您需要做的就是在服務器上創建一個自定義ServiceLocator,GWT將使用它來正確定位被調用的服務:
public class SpringServiceLocator implements ServiceLocator {public Object getInstance(Class clazz) {ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(RequestFactoryServlet.getThreadLocalServletContext());return context.getBean(clazz);}
}
第二步是像這樣在web.xml上聲明您的RequestFactory servlet,(我假設您已經進行了彈簧設置):
<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>
和GWT一樣,您必須配置代理(因為使用服務風格后端,因此必須使用ValueProxy而不是EntityProxy)和作為服務遠程接口的請求:
//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);}
集成就是這樣,有關如何使用RequestFactory API的更多信息,請參見: RequestFactory API
參考: Fancy UI博客中的JCG合作伙伴 Idriss Mrabti 使用RequestFactory API進行的Spring GWT集成 。
相關文章 :
- GWT 2 Spring 3 JPA 2 Hibernate 3.5教程– Eclipse和Maven 2展示
- GWT 2 Spring 3 JPA 2 Hibernate 3.5教程
- 建立自己的GWT Spring Maven原型
- 使用Spring 3.1和基于Java的配置引導Web應用程序,第1部分
- GWT Spring和Hibernate進入數據網格世界
- 使用Selenium或WebDriver測試GWT應用
- GWT,GWT-Ext(SmartGWT),GXT(Ext GWT)常見任務
- GWT EJB3 Maven JBoss 5.1集成教程
- SmartGWT入門,提供出色的GWT界面
翻譯自: https://www.javacodegeeks.com/2011/12/spring-gwt-integration-using.html