是否有与ASP.Net整合GWT的好工具吗? [英] Are there any good tools for integrating GWT with ASP.Net?

查看:164
本文介绍了是否有与ASP.Net整合GWT的好工具吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有什么好的工具可以使用GWT(谷歌网页工具包)与ASP.Net服务器应用程序?编程模型和工具,GWT是相当不错的,不过,这将是很好,如果后端可以保持在C#/ ASP.Net。

Are there any good tools available for using GWT (the Google Web Toolkit) with an ASP.Net server application? The programming model and tools for GWT are quite nice, however, it would be nice if the backend could remain in C#/ASP.Net.

是否有当前可用于一个很好的解决方案?

Is there currently a good solution available for this?

推荐答案

我的GWT项目工作,其中ASP.NET是我的服务器上的唯一选择。我发现,尽管它需要一些额外的工作,<一个href=\"http://googlewebtoolkit.blogspot.com/2008/08/getting-to-really-know-gwt-part-2.html\">JavaScript覆盖类型可以很容易。这不要紧,你的服务器使用的是什么技术,只要能与JSON序列数据进行响应。例如,如果你在C#中的联系人类:

I am working on a GWT project where ASP.NET is my only option on the server. I have found that, while it requires a little extra work, JavaScript Overlay types can make it easy. It doesn't matter what technology your server is using, so long as it can respond with JSON serialized data. For instance, if you have a Contact class in C#:

public class Contact 
{
  public int Id { get; set; }
  public string LastName { get; set; }
  public string FirstName { get; set; }
  public string Email { get; set; }
}

设计您的服务器,使其返回此序列化为JSON。我使用ASP.NET MVC这个,因为它需要这么少的工作。这里有一个很简单的例子,我们假设有联系,给定一个id,将返回一个实例,联系一个静态方法:

Design your server so that it returns this serialized as JSON. I use ASP.NET MVC for this because it requires so little work. Here's a very simple example where we'll assume that Contact has a static method that, given an id, will return a Contact instance:

public class ContactController : Controller 
{
  public ActionResult GetContact (int id) 
  {
    return Json(Contact.GetById(id), JsonRequestBehavior.AllowGet);
  }
}

现在,在您的GWT应用程序,为您的联系人创建一个JavaScript覆盖类型:

Now, in your GWT application, create a JavaScript overlay type for your Contact:

import com.google.gwt.core.client.JavaScriptObject;

public class Contact extends JavaScriptObject {
  protected Contact() { }

  public final native int getContactId() /*-{ return this.Id; }-*/;
  public final native String getLastName() /*-{ return this.LastName; }-*/;
  public final native String getFirstName() /*-{ return this.FirstName; }-*/;
  public final native String getEmail() /*-{ return this.Email; }-*/;

  public static final native Contact createFromJson(String json) /*-{
    return eval('(' + json + ')');
  }-*/;
}

接下来,在GWT项目,使用HTTP请求与服务器进行通信:

Next, in your GWT project, use an HTTP request to communicate with the server:

public class ContactLoader {
  private int id; 

  public ContactLoader(int id) {
    this.id = id;
  }

  public void beginLoad() {
    String url = "/YourService/GetContact/?id=" + id;
    RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);
    try {
      @SuppressWarnings("unused")
      builder.sendRequest(null, new RequestCallback() {
        @Override
        public void onResponseReceived(Request req, Response resp) {
          Contact contact = Contact.createFromJson(req.getText());
          // do something with your contact.  In my project, I
          // fire an event here for which the contact is the payload
        }

        @Override
        public void onError(Request request, Throwable exception) {
          // handle your error
        }
    catch (Exception exception) {
      // handle your error
    }
  }
}

另外,也可以使用通用的类型的覆盖的类型。例如,我永远不会返回裸对象,我总是用一个通用的集装箱运输,这样我可以轻松地处理客户端上的错误报告。

It is also possible to use generic types with overlay types. For example, I never return a bare object, I always use a generic container for transport so that I can easily handle error reporting on the client side.

C#:

public class ServerResponse<T> 
{
  public T Payload { get; set; }
  public bool Success { get; set; }
  public String Message { get; set; }
}

Java的:

public class ServerResponse<T extends JavascriptObject> 
  extends JavaScriptObject {
  protected ServerResponse() { }

  public final native T getPayload() /*-{ return this.Payload; }-*/;
  public final native boolean getSuccess() /*-{ return this.Success; }-*/;
  public final native String getMessage() /*-{ return this.Message; }-*/;
}

这让我回我联系,联系人或任何数组。它还有助于你的数据加载逻辑,减少重复。

This lets me return my Contact, an array of Contacts or whatever. It also helps in reducing duplication in your data loading logic.

这是一个过于简单的例子,但我希望它足以扫清道路的人需要与非Java后端帮助。我发现的有用的讨论覆盖类型另一个资源是 JSON解析与GWT JavaScript的覆盖类型由Matt Raible。

This is an overly simple example, but hopefully it is enough to clear a path for anyone needing help with a non-Java back-end. Another resource that I found helpful that discusses overlay types is "JSON Parsing with JavaScript Overlay Types in GWT" by Matt Raible.

这篇关于是否有与ASP.Net整合GWT的好工具吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆