2014-09-30 96 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); 
} 

}