简体   繁体   English

使用GWT将数据对象从客户端传输到服务器以保持数据的方式是什么?

[英]What's the way you transfer data object from client to server for persisting datas using GWT?

If you use JPA or another kind of persistence, you probably have a way to save a record that is sent to client via ajax. 如果您使用JPA或其他类型的持久性,您可能有办法保存通过ajax发送到客户端的记录。

EDIT : the interface is done with GWT, so all ajax call are classic java method (converted to javascript equivalent) 编辑:接口是用GWT完成的,所以所有ajax调用都是经典的java方法(转换为javascript等价)

Let's take the class Person which is an entity in database. 让我们把类Person作为数据库中的实体。 Person has four fields : name, birthday, id, email 人有四个字段: name, birthday, id, email

When you load a person from server via ajax, you generally send to client a Person object. 当您通过ajax从服务器加载某人时,通常会向客户端发送一个Person对象。

In your Person editor, you display the name, the birthday and the email. 在人物编辑器中,您可以显示姓名,生日和电子邮件。 When the Person object is edited you may want to display the id. 编辑Person对象时,您可能希望显示id。

There are two cases : 有两种情况:

  • save person : only email can be changed but you can display id 保存人:只能更改电子邮件,但您可以显示ID
  • create person : email, name and birthday can be changed 创建人:可以更改电子邮件,姓名和生日

When you send the just edited datas to server, what's the way you proceed ? 将刚刚编辑的数据发送到服务器时,您的进展方式是什么?

I see several approaches : 我看到几种方法:

  • send a Person object. 发送一个Person对象。 In this case you must take care of data you persist and not only make the person object you received from client to persist mode because a hacker can send data you may not want to be changed (and you can't trust on interface to disabled these changes). 在这种情况下,您必须处理您持久存储的数据,而不仅仅是让您从客户端收到的人物对象持久化模式,因为黑客可以发送您可能不想更改的数据(并且您不能信任接口以禁用这些数据)变化)。 In this case, there is several approaches too : 在这种情况下,也有几种方法:
    • use two functions (save and create), create a new Person object on server (or load the persisted instance via the id if you are in the save method) and copy all fields you want from the client Person object to the persisted one 使用两个函数(保存和创建),在服务器上创建一个新的Person对象(如果你在save方法中,则通过id加载持久化实例)并将你想要的所有字段从客户端Person对象复制到持久化的对象
    • use one function (saveOrCreate) and check if id exists. 使用一个函数(saveOrCreate)并检查id是否存在。 It is equivalent to the above approach by merging the two functions in one with a big "if" 它相当于上面的方法,将两个函数合并为一个大的“if”
  • send datas to the server (email, birthday, name, id (in case of edit)). 将数据发送到服务器(电子邮件,生日,姓名,ID(编辑时))。 When you do that, create a new Person object (or load the persisted one) and copy datas to this persisted instance. 执行此操作时,创建一个新的Person对象(或加载持久的对象)并将数据复制到此持久化实例。

To summary you have one of the following method signature (just for edit case): 总结一下,您有以下方法签名之一(仅适用于编辑案例):

  • Person savePerson(Person person);
  • Person savePerson(String id, String email);

I see pros and cons for each approach. 我看到每种方法的利弊。 For example the first allow quick change on the Person model without modifying all savePerson calls. 例如,第一个允许快速更改P​​erson模型而不修改所有savePerson调用。 But it is less readable than the second approach to know what is really saved. 但它比第二种方法更难以理解什么是真正节省的。

I don't know what is the best one and if you know another way to do that. 我不知道什么是最好的,如果你知道另一种方法。 So, how do you do ? 那么,你好吗?

It is always nice to use domain objects for data transfer. 使用域对象进行数据传输总是很好。 In GWT you have to define your interfaces and mark your Rpc BeanProxies. 在GWT中,您必须定义接口并标记Rpc BeanProxies。 As part of the project evolution, you find a need to add additional fields to an object. 作为项目演变的一部分,您需要向对象添加其他字段。 If you go with interfaces that use data attributes rather than objects, you will be constantly changing them and the method signature becomes clumsy and unreadable. 如果使用的是使用数据属性而不是对象的接口,则会不断更改它们,并且方法签名变得笨拙且不可读。

However, certain people ask for a separation of persistance domain entities from business domain. 但是,某些人要求将持久域实体与业务域分开。 In such cases, property copying (apache commons-bean utils) can be used. 在这种情况下,可以使用属性复制(apache commons-bean utils)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用fileUpload在GWT中将文件从客户端传输到服务器 - How to transfer a file From Client to Server in GWT using fileUpload 如何从GWT的客户端向服务器发送Exception对象? - How to send an Exception object from GWT's client to server? 您使用什么来进行客户端与GWT的服务器通信? - What do you use for client to server communication with GWT? 如何从服务器(appengine)获取日期对象并将其转换为客户端的时区日期(gwt)? - How can I get a date object from server (appengine) and convert it to client's timezone's date (gwt)? 从数据源检索UTC日期时间 - 将其放入Joda DateTime对象并将其持久保存到MongoDB。 什么是最好的方法? - Retrieving a UTC datetime from a data source — putting it into a Joda DateTime object and persisting it to MongoDB. What is the best way? 使用服务器中的数据填充GWT中的字典对象 - Populating a Dictinoary object in GWT with data from server 在服务器和客户端套接字之间传输XML数据的最佳方法 - Best way to transfer XML data between server and client sockets GWT / GXT使用服务器端和客户端上的对象-允许什么? - GWT/GXT using objects on server vs. client — what is allowed? 客户端服务器之间的对象传输 - Object transfer between client server GAE / GWT服务器端数据不一致/在实例之间不持久 - GAE/GWT server side data inconsistent / not persisting between instances
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM