将GWT与Spring集成

时间:2014-09-30 12:50:24

标签: java spring maven gwt

我知道这个话题已多次讨论,但我发现大部分信息都不是最新的。

我正在寻找有关如何将GWT与Spring框架集成的教程/示例。 我发现了很多examplex(其中一些甚至工作),但只有较旧的库。我正在寻找最新库(或至少与最新库兼容)的解决方案。

还有很多例子使用spring4gwt库(用于创建" glue" servlet) - 还有另外一种方法吗?

我想使用GWT + Spring + Hibernate + Maven创建简单的示例应用程序。我从创建Web Application Project(来自Eclipse)开始。我将项目转换为Maven项目。说实话,我被困在这里。我可以创建简单的服务(+ async),但不知道如何配置适当的servlet并进一步。示例我在spring4gwt上发现了relay,但我不想使用它(自2009年以来我没有新版本)。

如果有人能够逐步解释整合,那就太好了。

很抱歉,如果这个是重复的,但经过长时间的搜索,我找不到符合我需要的明确解决方案。

3 个答案:

答案 0 :(得分:2)

您有很多方法可以与Spring集成,但我认为最好的选择是使用RestyGWT Framework

由于您使用HTTP协议和JSON格式来序列化对象,因此使用Spring ControllersRestyGWT进行通信无法解决问题。

您也可以使用自己的控制器来回复GWT RPC Requests。您可以使用GWT Dispatcher而不是使用Spring MVC Request Dispacher,并将控制器上的URL映射到GWT客户端中的服务。

如果您使用RESTY GWT API,则可以编写界面,使用JAX-RS@POST, @GET, @DELETE, @PathParam注释等方法映射方法。

以下是我使用RestyGWT对项目进行的操作:

该项目由2个项目组成: 项目客户端 项目服务器

客户端包含与GWTRestyGWT相关的所有文件。 服务器使用Spring包含来自后端实现的所有文件。

Maven overlay用于在程序包编译阶段合并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);
}

}

答案 1 :(得分:0)

我用这个设置创建了很多项目,你不需要spring4gwt! 我的解决方案是使用“桥”类,允许您调用像spring controller一样的异步服务:

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 :(得分:0)

看一下与Spring集成的gwtp源代码。我相信你会在那里找到一个可能的答案:

https://github.com/ArcBees/GWTP

相关问题