Question

I have a Google Web Toolkit (GWT) application and when I link to it, I want to pass some arguments/parameters that it can use to dynamically retrieve data. E.g. if it were a stock chart application, I would want my link to contain the symbol and then have the GWT app read that and make a request to some stock service. E.g. http://myapp/gwt/StockChart?symbol=GOOG would be the link to my StockChart GWT app and it would make a request to my stock info web service for the GOOG stock.

So far, I've been using the server-side code to add Javascript variables to the page and then I've read those variables using JSNI (JavaScript Native Interface).

For example:

In the host HTML:

<script type="text/javascript">
  var stockSymbol = '<%= request.getParameter("symbol") %>';
</script>   

In the GWT code:

public static native String getSymbol() /*-{
    return $wnd.stockSymbol;
}-*/;

(Although this code is based on real code that works, I've modified it for this question so I might have goofed somewhere)

However, this doesn't always work well in hosted mode (especially with arrays) and since JSNI wasn't around in version 1.4 and previous, I'm guessing there's another/better way.

Was it helpful?

Solution

If you want to read query string parameters from the request you can use the com.google.gwt.user.client.Window class:

// returns whole query string 
public static String getQueryString() 
{
    return Window.Location.getQueryString();
}

// returns specific parameter
public static String getQueryString(String name)
{   
    return Window.Location.getParameter(name);
}   

OTHER TIPS

It is also a nice option to 'parameterize' a GWT application using hash values.

So, instead of

 http://myapp/gwt/StockChart?symbol=GOOG

use

 http://myapp/gwt/StockChart#symbol=GOOG

There is some nice tooling support for such 'parameters' through GWT's History Mechanism.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top