Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I share a class between GWT client code and server code?

Tags:

java

gwt

Is it possible to "share" a Java class between GWT client code and server code?

I have a simple class that just holds a few Strings and a few List<String>'s. I'd like to be able to use this class in both client side code and server side code just for some consistency.

Just to give some background, the class I'm trying to share is just a container for an error message with some extra info. In my application there are some errors that prevent a save so we do those checks on the client side (before saving). Then after saving, we do more "validation" checks on the saved object on the server side.

I understand that it would have to be "compiled" twice, once for the GWT compile and once for the normal javac compile, but it seems like that should be possible.

Is there a way to set up my GWT project to do this?

like image 808
cshanes Avatar asked Jan 13 '12 18:01

cshanes


1 Answers

One of the reason of using GWT is to have single domain model for client and server.

It is a common practice to have shared classes between server code and client side.

Usually project with GWT client source code splits up to three packages

com.yourpackage
               -shared
               -client
               -server

where shared package compiles both to java byte code and javascript, client to javascript, and server side logic is placed in server package.

Compilation path for client can be configured in *.gwt.xml file.

like image 200
Mairbek Khadikov Avatar answered Oct 04 '22 22:10

Mairbek Khadikov