Java – what is the way to transfer data objects from the client to the server using GWT to maintain data?

If you use JPA or other types of persistence, you may have a way to save records sent to the client through Ajax

Edit: the interface is completed with GWT, so all Ajax calls are classic java methods (converted to JavaScript equivalent)

Let's take the class person as an entity in the database People have four fields: name, birthday, ID card, e-mail

When you load someone from the server via Ajax, you usually send a person object to the client

In the character editor, you can display name, birthday and email When editing the person object, you may want to display the ID

There are two situations:

>Save by: you can only change email, but you can display ID > Creator: you can change email, name and birthday

How do you progress when sending the data you just edited to the server?

I see several ways:

>Send a person object In this case, you have to deal with your persistent data, not just the persistence mode of people objects you receive from the client, because hackers can send data you may not want to change (and you can't trust the interface to disable these data) In this case, there are several methods:

>Use two functions (save and create) to create a new person object on the server (if you are in the save method, load the persistent instance through ID) and copy all the fields you want from the client person object to the persistent object > use one function (saveorcreate) and check whether the ID exists It is equivalent to the above method, combining the two functions into a large "if"

>Send data to the server (e-mail, name, ID (when editing)) When you do this, create a new person object (or load a persistent object) and copy the data to this persistent instance

To summarize, you have one of the following method signatures (only applicable to editing cases):

>Saveperson (person); > Person savePerson(String id,String email);

I see the pros and cons of each approach For example, the first allows you to quickly change the person model without modifying all saveperson calls But it is more difficult than the second method to understand what real savings are

I don't know what's best if you know another way So, how are you?

Solution

It is always good to use domain objects for data transfer In GWT, you must define the interface and mark RPC beanproxies As part of the project evolution, you need to add additional fields to the object If you use interfaces that use data properties instead of objects, they are constantly changed and the method signature becomes clumsy and unreadable

However, some people require that persistent domain entities be separated from business domains In this case, you can use attribute replication (APACHE commons bean utils)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>