I have been attempting to do the following for a few days to no avail:

I have a GWT application that makes several RPC calls on startup to load initial data. For the most part, this data is relatively static, so I'd skip the RPC call if I could. But the data changes more often than the application does, so I'd rather not include it at compile time.

Instead, I take the data and store it in JSON in a static .js file. I include that in the loading HTML for the application:

html
... GWT loading stuff...>

script src="staticstuff.js">/script> //this is intentionally wrong so it will show on stackoverflow
html
with static stuff looking like:

I could pull back the js objects and parse them into my object graph myself, but I'd prefer not to, especially since GWT already created a converter for me (since OurMoreComplexObject) is already returned in RPC calls.

Has anyone attempted something like this before? If so, how do I get it to work? Is there some other way to do this? (The main goal is that if the data is stored in a file, I can load it onto our CDN instead of forcing the call back to our servers at startup every time)

3 Answers
3

The basics of it are that you make the call when your server is rendering the page, and get GWT to serialize it to it's normal RPC form, and store it in the page in a javascript variable. From there you can read it in. The link above should hopefully have enough detail for you.

The main benefit being that your client gets the page all in HTML/js and doesn't need to make an AJAX call to get more data.

and to answer the OP's question, you can just intercept the RPC call, and cache the response to a file statically, and on the next call, instead of allowing the rpc call to go through, you can just return what was cached.
–
ChiiFeb 12 '09 at 10:02

Why are you doing all these strong-arming when you could use the Dictionary class? To create a dynamic GWT-host page, simply use a JSP as the GWT-host page and write all your javascript objects into it. Then when module loads, use the Dictionary class to read those javascript objects.
–
Blessed GeekJan 6 '12 at 4:54

If you want to stick with JSON (and not serialize an RPC response) than you can use the JavaScriptObject wrapper a.k.a. JSO. This is a great way to get complex object graphs from JSON w/o having to write a bunch of marshaling code.
It follows this pattern:

The good news is that this has semi-magical properties in that your 'getStaticData' method will just work. The bad news is that you can't treat those JSOs as easily as POJOs. Dealing with list iteration (via JSArray) can be a particular pain.