Complex response after submit a FormPanel - How to

1,447 views
Skip to first unread message

Zé Vicente

unread,
Aug 25, 2009, 10:09:51 AM8/25/09
to Google Web Toolkit
Hello all,

This thread is about the result we get back after we submit a
FormPanel. I'm using a FormPanel + FileUpload in order to upload
photos in my GWT app.

When I submit the form, there is a servlet that takes care of the
request and saves the image on server side. I also have an object
called "Image" (from my business model) that is created to represent
the file just uploaded. This object contains the path of the image, a
small description and etc. I need to have this object sent back to the
UI.

So, what is the best way to get a reference to my object model "Image"
after form submission? Using FormPanel I am able to provide a handler
that will give access to the method x.getResults() which returns only
String generated by my servlet.

What is your experience with that?

Regards,
José Vicente

Manuel Carrasco Moñino

unread,
Aug 25, 2009, 10:57:06 AM8/25/09
to google-we...@googlegroups.com
You can read and process the server's response.
In this string your server can write the information your client needs using text, xml or json and you can parse it using regular expresions,  com.google.gwt.json.JSON or com.google.gwt.xml.XML.

SubmitCompleteHandler onSubmitFormCompleteCallback = new SubmitCompleteHandler() {
        public void onSubmitComplete(SubmitCompleteEvent event) {
            String serverResponse = event.getResults();
            [ here process the response ]
        }
}


Manolo Carrasco

Thomas Broyer

unread,
Aug 25, 2009, 11:12:26 AM8/25/09
to Google Web Toolkit
Our servlets send JSON (with "Content-Type: text/html" though, so that
the SubmitCompleteEvent is reliably fired...) and we then parse
e.getResults() on client-side.
We're using JSON for all our server responses so the FormPanel
integrates quite well with our other code based on RequestBuilder
(you'd have guess: we don't use GWT-RPC).

The issue you'll face with FormPanel (compared to RequestBuilder) is
that there's no "error handler" (no onError / onFailure) and you
cannot get the response status code either (as in
Response.getStatusCode() when using RequestBuilder). So your server
has to generate JSON (with content-type: text/html) to convey errors
too, and your client code has to inspect the returned JSON to
determine whether it's a success or failure.
Our servlets in case or error always return a JSON object (we call it
"status object") with the same properties ("statusCode", "message",
etc.) mimicking HTTP-level information, and we baked the handling of
the response in a common SubmitCompleteHandler wrapped around a
RequestCallback/AsyncCallback-like callback (it parses the JSON, looks
for "statusCode" and "message" properties; if they're present and
"statusCode >= 400", then it calls the callback.onError(statusObject),
otherwise it calls callback.onSuccess(parsedJsonObject)).

Zé Vicente

unread,
Aug 25, 2009, 11:22:04 AM8/25/09
to Google Web Toolkit
Sounds good to me! I am used to the rpc calls and I did not work with
json yet.

Seems that I have to do it! Thank you both for your time!

Regards,
Zé Vicente

mars1412

unread,
Aug 25, 2009, 12:17:17 PM8/25/09
to Google Web Toolkit
just an alternative, if you don't want to go the clean, sophisticated
way:
you could simply return the id and then start an RPC to get the Image
object for this id

this is of course nasty, because you need another roundtrip to the
server - but if you need a quick and easy way. well..
Reply all
Reply to author
Forward
0 new messages