Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice for parameterizing GWT app?

Tags:

gwt

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.

like image 937
KC Baltz Avatar asked Sep 23 '08 16:09

KC Baltz


2 Answers

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);
}   
like image 184
Drejc Avatar answered Nov 05 '22 11:11

Drejc


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.

like image 22
mxro Avatar answered Nov 05 '22 13:11

mxro