2014-09-30 95 views
4

我知道這個話題已經被多次討論過了,但是我發現大部分的信息並不是最新的。將GWT與Spring集成

我正在尋找關於如何將GWT與Spring框架集成的教程/示例。 我發現了許多examplex(其中一些甚至可以工作),但只能使用較舊的庫。我正在尋找最新的庫(或至少與最新的兼容)的解決方案。

也有很多例子使用spring4gwt庫(用於創建「膠水」servlet) - 有沒有另一種方式?

我想用GWT + Spring + Hibernate + Maven創建簡單的示例應用程序。我開始創建Web Application Project(來自Eclipse)。我將項目轉換爲Maven項目。說實話我被困在這裏。我可以創建簡單的服務(+異步),但不知道如何配置適當的servlet並繼續前進。例子我在spring4gwt上發現了relay,但我不想使用它(自2009年以來沒有新版本)。

如果有人能夠逐步解釋集成,那將會很棒。

對不起,如果這是一個重複的,但經過長時間的搜索,我還沒有找到明確的解決方案,適合我的需求。

回答

0

我用這個設置創建了很多項目,你不需要spring4gwt! 我的解決辦法是使用的「橋樑」類,允許您調用像春天控制器的異步服務:

import javax.servlet.ServletContext; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

import org.apache.log4j.Logger; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.context.ServletContextAware; 
import org.springframework.web.servlet.ModelAndView; 

import com.google.gwt.user.server.rpc.RemoteServiceServlet; 

public abstract class BaseRemoteService extends RemoteServiceServlet implements 
     ServletContextAware { 

    private static final long serialVersionUID = 2470804603581328584L; 
    protected Logger logger = Logger.getLogger(getClass()); 
    private ServletContext servletContext; 

    @RequestMapping(method = { RequestMethod.GET, RequestMethod.POST }) 
    public ModelAndView handleRequest(HttpServletRequest request, 
      HttpServletResponse response) throws Exception { 
     doPost(request, response); 
     return null; // response handled by GWT RPC over XmlHttpRequest 
    } 

    @Override 
    public void setServletContext(ServletContext servletContext) { 
     this.servletContext = servletContext; 
    } 

    @Override 
    public ServletContext getServletContext() { 
     return this.servletContext; 
    } 
} 

現在,你的* RpcServiceImpl應該是這樣的:

@Controller 
@RequestMapping("/*/action.service") 
public class ActionRpcServiceImpl extends BaseRemoteService implements ActionRpcService { 
    //this class is managed by spring, so you can use @Autowired and other stuffs 
    //implementation of your rpc service methods, 
} 
2

你有很多方法來與Spring集成,但我認爲最好的辦法是使用RestyGWT Framework

由於您使用HTTP協議和JSON格式以便序列化對象,您將不會遇到使用RestyGWTSpring Controllers交流的問題。

您也可以使用您自己的控制器來響應GWT RPC Requests。您可以使用Spring MVC Request Dispacher而不是使用GWT Dispatcher,並將控制器上的URL映射到GWT客戶端中的服務。

如果使用RESTY GWT API,你可以只寫你的接口,採用JAX-RS註解像@POST, @GET, @DELETE, @PathParam映射方法等

下面是我使用RestyGWT做我的項目是什麼:

項目是組成的2個項目: 項目客戶 項目服務器

客戶端包含與GWTRestyGWT所有文件。 服務器包含使用Spring的後端實施中的所有文件。

Maven覆蓋用於合併包編譯階段的2個項目,因此您最終會與GWT * js文件和服務器文件進行最終的爭奪。

要使用RestyGWT你必須創建誰伸出RestService接口:

public interface MyRestService extends RestService{ 
    @GET 
    @Path("/foo") 
    public void getFoo(MethodCallback<List<Foo>); 
    @POST 
    @Path("/foo") 
    public void saveFoo(Foo foo ,MethodCallback<MessageResponse>); 
} 

要使用這項服務,你寫的東西是這樣的:

MyRestService service = GWT.create(MyRestService.class); 

,你就會有這樣的事情使用服務:

service.getFoo(new MethodCallBack<List<Foo>>(){ 
    public void onSucess(List<Foo> foos){ 
    /* You will get foos, you dont have to worry about serialization, RESTYGWT does it for you */ 
} 
public void onError() ... 
}); 

而且喲ü將有一個控制器,以這樣的這一要求作出迴應:

@Controller 
class myController{ 

@Autowired FooService svc; 

@RequestMapping(value = "/foo", method = RequestMethod.GET, produces= "application/json") 
public @ResponseBody List<Foo> getAllFoos(){ 


    return svc.all(); 
} 
@RequestMapping(value = "/foo", method = RequestMethod.POST, produces= "application/json", consumes="application/json") 
public @ResponseBody MessageResponse save(@ResponseBody Foo foo){ 
    svc.save(foo);  
    return new MessageResponse("Foo saved with sucess", 200); 
} 

}