服务器可以创建GWT对象并将其返回给客户端吗? [英] Can the server create and return GWT objects to the client?

查看:54
本文介绍了服务器可以创建GWT对象并将其返回给客户端吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仍然在与GWT和App Engine搏斗,而我遇到了这个问题:

我有一个应用引擎,其中填充了各种数据,我想使用GWT RPC在客户端上展示这些数据.

我已经发现了一种很难的方法,因为我的Model对象是用JDO注释的,所以我不能仅仅将它们发送回客户端,因为它们不可序列化.这意味着我将不得不创建一层中间类来从我的模型对象中提取数据,并将其发送回客户端以异步使用.

我想知道,是否可以在servlet中构造GWT对象并将其发送回使用?例如,该servlet将接收我的异步请求,从数据库中提取我想要的数据,创建一个具有适当子元素的GWT VerticalPanel()并将数据发送回客户端以进行注入.>

我对这里正在进行的Java/Java脚本交互的理解仍然模糊不清,并且我认为在应用程序交付后发送未编译为Javascript的Java对象将无法正常工作.有人可以帮我澄清一下吗?

解决方案

没有服务器不能创建用于表示层的GWT UI对象(如垂直面板),也不应该,这就是为什么它被称为服务器"和表示层",其中一个提供数据并处理所有业务逻辑,另一个则在屏幕上显示内容并允许用户与其交互.

不过,您可以将JPA批注的POJO发送到前端(我们在所有应用程序中都可以这样做).您只需要包括注释本身的源代码,以便GWT知道如何编译它们.您还需要确保您的POJO位于由NameOfXmlFile.gwt.xml文件引用的包中,例如:

 < module>< inherits name ='com.google.gwt.user.User'/>< source path ="domain"/></module> 

在我的情况下,此文件位于一个名为"domain"的程序包上方的文件夹中,我的所有JPA均在该文件夹中标记POJO的实时状态.然后在您的客户端,告诉它继承该.gwt.xml文件:

 < module>< inherits name ='com.google.gwt.user.User'/><!-域层引用->< inherits name ='your.package.structure.NameOfXmlFile'/></module> 

您可以在这些类中放入什么内容(例如不支持BigDecimal等)受到限制,但是可以发送GWT编译器可以编译的任何内容(当然可以包含JPA批注),而无需任何方式转移对象.这是GWT的真正优势之一,您可以在整个应用程序中使用相同的JPA Pojo,而无需创建任何其他类似的对象.

编辑:我只是注意到您说的是 JDO ,而不是 JPA .我认为同样适用于那里,即使它们只是注释?

Still wresting with GWT and App Engine, and I have come to this problem:

I have an app engine populated with various data, which I would like to present on the client using a GWT RPC.

I have found out the hard way that, because my Model objects are annotated with JDO, i can't just send them back to the client because they aren't serializable. This means I'm going to have to create a layer of intermediate classes to extract data from my model objects, and send it back to the client to use asynchronously.

I am wondering though, it is possible to construct a GWT object in a servlet and send it back to be used? For example, the servlet would be receive my asynchronous request, pull out the data i want from the database, create a GWT VerticalPanel() with appropriate child elements for the data, and send that VerticalPanel back to the client to be injected.

My understanding of the Java / Javascript interaction that is going on here is still foggy, and I'm thinking that sending a Java object that is not compiled to Javascript after the application is deplyed will not work. Can anybody clarify this for me?

解决方案

No the server can't create GWT UI objects (like vertical panels) to be used in the presentation layer, nor should it, that's why it's called a 'server' and a 'presentation layer' one serves the data and handles all the business logic, the other displays things on a screen and allows a user to interact with them.

You can however send your JPA annotated POJO's to the front end just fine (we do it in all our applications). You simply need to include the source code for the annotations themselves so that GWT knows how to compile them. You also need to make sure your POJOs's are in a package that is referenced by a NameOfXmlFile.gwt.xml file, eg:

<module>
    <inherits name='com.google.gwt.user.User'/>
    <source path="domain" />
</module>

This file in my case is in a folder above a package called 'domain' where all my JPA annotated POJO's live. Then in your client side you tell it to inherit that .gwt.xml file:

<module>
        <inherits name='com.google.gwt.user.User'/>
        <!-- Domain layer references -->
        <inherits name='your.package.structure.NameOfXmlFile'/>
</module>

There are restrictions on what you can put in those classes (like for example BigDecimal is not supported, etc) but anything that can be compiled by the GWT compiler (and JPA annotations certainly can be) can be sent without needing any kind of transfer objects. This is one of the real strengths of GWT, that you can use the same JPA Pojos in your entire application, without ever needing to create any other similar object.

Edit: I just noticed you said JDO and not JPA. I assume the same applies there as well though if they are just annotations?

这篇关于服务器可以创建GWT对象并将其返回给客户端吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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