Sharing HashMap keys between server and client

13 views
Skip to first unread message

Billy

unread,
Jul 30, 2008, 12:28:51 PM7/30/08
to Google Web Toolkit
I'm using GWT 1.5 and I'm transferring a HashMap from Server to
Client. Currently I have to define a key once on the server side and
again on the client side. Is there some way that I can create a
single file that holds all of the keys which is shared by both the
client an the server?

Example:
Server side
HashMap<String,String> props = new HashMap<String, String>();
props.put("KEY1","VALUE1");
return props;

Client side
props.get("KEY1")

Notice how "KEY1" is a literal on both sides. I would like to put
these kind of literals into a common file that both server and client
could access if possible.

Paul Robinson

unread,
Jul 30, 2008, 12:36:12 PM7/30/08
to Google-We...@googlegroups.com
public interface Keys {
public String KEY1 = "Key1";
}

HashMap<String,String> props = new HashMap<String, String>();

props.put(Keys.KEY1, "VALUE1");

props.get(Keys.KEY1);

WadeC

unread,
Jul 30, 2008, 12:54:30 PM7/30/08
to Google Web Toolkit
The easiest solution is to place the constants values in a separate
java class file in the client package. You will need to import the
class on the server side to use it ... but it should work.

Another option is to place these constants in their own package and
import the class into both the client and the server... but you will
also need to create a new gwt.xml file for this new GWT module and
then inherit this in the main gwt.xml - which gets a little more
complicated...

The final (and least recommended method) would be to place it on the
server side ... but I find this option fairly dangerous ... if you
start importing server side classes that perform java operations that
the GWT does not support you can get yourself into trouble...

WadeC
Reply all
Reply to author
Forward
0 new messages