how to pass hash map to native java script method

947 views
Skip to first unread message

rsutton

unread,
Jun 8, 2011, 9:19:05 PM6/8/11
to Google Web Toolkit
I am trying to pass a Hashmap to a native method as can be seen below.
I have been unable to find any suitable example code. Any assistance
would be greatly appreciated.

I know this is simple, I just haven't done it before.

Thanks.


public void onClick(ClickEvent event)
{
Map<String, String> params = new HashMap<String, String>();
params.putAll(definition.getParams());

.... this is where my problem is, how to convert the params
object to a suitable
object to work with the native openReportWindow method

openReportWindow(definition.getReportServletName(), params);
}

private static native void openReportWindow(String action, values) /*-
{
var form = document.createElement("form");
form.setAttribute("method", "post");
form.setAttribute("action", action);
// setting form target to a window named
'formresult'
form.setAttribute("target", "_blank");
for (var i=0; i<values.length; i++) {
var hiddenField = document.createElement("input");
hiddenField.setAttribute("name", values[i].name);
hiddenField.setAttribute("value", values[i].value);
form.appendChild(hiddenField);
}
document.body.appendChild(form);
form.submit();
document.body.removeChild("form");
}-*/;

Derek

unread,
Jun 10, 2011, 10:03:13 AM6/10/11
to Google Web Toolkit
Actually I don't think it's as simple as you'd think. Gwt java objects
are pretty opaque to JSNI. Depending on what you need, i'd suggest
using JSONObject as a replacement for HashMap. That should probably
work for you.

Thomas Broyer

unread,
Jun 10, 2011, 11:32:55 AM6/10/11
to google-we...@googlegroups.com
Wouldn't it be easier to rewrite your "openReportWindow" in Java rather than JSNI?


On Thursday, June 9, 2011 3:19:05 AM UTC+2, rsutton wrote:
  var form = document.createElement("form");

FormElement form = Document.get().createFormElement();
 
  form.setAttribute("method", "post");

form.setAttribute("method", "post");
or
form.setMethod("post"); (would be the equivalent of "form.method='post'" in JS)
 
  form.setAttribute("action", action);
    // setting form target to a window named
'formresult'
  form.setAttribute("target", "_blank");
    for (var i=0; i<values.length; i++) {

for (Map.Entry<String, String> entry : values) {
 
       var hiddenField = document.createElement("input");

InputElement hiddenField = Document.get().createHiddenInputElement();
 
                hiddenField.setAttribute("name", values[i].name);

input.setName(entry.getKey());
 
             hiddenField.setAttribute("value", values[i].value);

input.setValue(entry.getValue());
 
                form.appendChild(hiddenField);

form.appendChild(hiddenField);
 
  }
  document.body.appendChild(form);

Document.get().getBody().appendChild(form);
 
  form.submit();

form.submit();
 
  document.body.removeChild("form");

form.removeFromParent(); // this is a very handy shortcut provided by GWT!
 
  }-*/;

 

rsutton

unread,
Jun 10, 2011, 10:38:43 PM6/10/11
to Google Web Toolkit
when I do a post from gwt, the generated html page is returned in an
object. there are 2 problems with this.

1. I want the browser to render the returned page
2. this report could be excess of 1GB.

rsutton

unread,
Jun 10, 2011, 10:41:54 PM6/10/11
to Google Web Toolkit
OK, I'll try this.

Thanks

Thomas Broyer

unread,
Jun 11, 2011, 3:36:35 AM6/11/11
to google-we...@googlegroups.com
I wasn't talking about FormPanel and its SubmitCompleteEvent. Go back read my previous message, I gave you almost all the code (if not all).

Thomas Broyer

unread,
Jun 11, 2011, 3:39:15 AM6/11/11
to google-we...@googlegroups.com
Also, you can get a FormPanel to post to target="_blank" if you like (or target="whatever", or a NamedIframe widget if you prefer not opening a window/tab).

rsutton

unread,
Jun 12, 2011, 7:14:44 AM6/12/11
to Google Web Toolkit
My Apologies and thank you Thomas. This is perfect. I failed to see
that you had added the alternate code to the quote.

Thanks again.

Robert.
Reply all
Reply to author
Forward
0 new messages