Development
Tips about software and web development
POSTing data via GWT
Posted by Manolis Platakis on 10-08-2010 at 0:52
This is my second GWT tip blog post. You can read the first one here, if you are interested in finding out how to call an iFrame's javascript function via GWT. In this post we are going to deal with sending POST data via GWT code. Recently, I had to send some POST data from a custom widget that I had developed to a totally different (non GWT) web page. To do so, I employed a GWT FormPanel.
FormPanel form = new FormPanel("_blank");
//form = new FormPanel("SomeNamedFrame");
form.setAction(Constants.FORMURL);
form.setMethod(FormPanel.METHOD_POST);
form.setEncoding(FormPanel.ENCODING_URLENCODED);
The above method calls are pretty self explanatory. The constructor argument is the "target" attribute in the POST HTML code. The setAction method sets the "action" attribute i.e. the web page that will receive and process the sent data.

I didn't want any actual form to show up but I was obliged to use one of the widgets that are associated with the FormPanel. So this is what I did.
TextArea tarea = new TextArea();
tarea.setName("tarea");
tarea.setVisible(false);
form.add(tarea);
form.setVisible(false);
add(form);
As you can see both the textarea and the form as a whole are not visible to the user. The final step was to send my data.
// listener to invoke the form submission
tarea.setText(dataToSendAsString());
form.submit();
All you have to do in the ACTION page is to read the POST parameter "tarea" (or whatever the textarea's setName argument was) and handle the received data. You can easily use a delimiter to split your received string.

I hope the above mini tutorial was helpful. Let me know what you think in the comments.
Accessing an iframe's JavaScript function via GWT
Posted by Manolis Platakis on 18-07-2010 at 20:13
During the past 2 years I've been coding a lot with Google Web Toolkit (GWT), watching it as it evolves into a powerful rapid web development framework enabling traditional system/software developers (like myself) to code Java and get JavaScript code cross-browser compatible. This is what GWT is all about; you code your web application in Java and you compile to fully functioning JavaScript code. Its architecture provides the developer with a very easy way to integrate the client side application with server side RPC calls. Along with the several widgets that come with GWT and a bunch of existing GWT libraries, like Smart GWT, GWT can become a powerful tool in one's hand. But powerful as it may be, there are times that you need to write native JavaScript in order to achieve what you want. GWT offers an easy way to mix Java and JavaScript, called JSNI. Recently, I had to call a function of a separate iframe that I had embedded in my GWT application. Coming up next, how it's done, in case anyone needs it.

First of all make sure you use a GWT NamedFrame like this:
NamedFrame myFrame = new NamedFrame("exampleFrame");
DOM.setElementAttribute(myFrame.getElement(), "id",
myFrame.getName());
myFrame.nativeFunct();
Next, this is how you code your JavaScript native function to call your iframe's function.
public native void nativeFunct() /*-{
var f = $wnd.frames["exampleFrame"];
f.contentWindow.myFunction();
}-*/;
Notice how in GWT you have to use $wnd to access the window element (and $doc to access the document element). The final step is to make sure that the JavaScript function myFunction is publicly visible to the window object in our frame's HTML/JS code.
var myFunction = new function() 
{
/* swap the alert with your actual logic */
alert("It's working!");
return;
}
Of course both your JSNI and JavaScript functions can also accept arguments. Last but not least, it's always best if both the iframe and the GWT application operate in the same web server.
Sources: 1, 2
Kudos: To this website for helping me out with the formatting of my source code.