Passing parameters to GWT directly from browser

636 views
Skip to first unread message

orshoe

unread,
Jan 2, 2008, 6:09:31 PM1/2/08
to Google Web Toolkit
Hi,

I have an application built in GWT, and it loads with a specific URL,
say www.somebank.com. I have a requirement to directly load different
'state' of the app based on the initial request parameters. For
example, I need to directly take the user to the 'customer' view if
the url is 'www.somebank.com/?customer=1234' or say, the 'summary'
view if the url is 'www.somebank.com/?month=12'.

Within the application this is achieved by using search boxes and
events thrown based on the search or mouse clicks. Even if I were to
load the application and then throw the associated event (on load for
example), how do I make GWT aware of the request parameters directly
from the URL. Or is there any better approach.

I appreciate any/all ideas/advise/discussion in this regards.

Thanks,
Oguntona

Ian Bambury

unread,
Jan 2, 2008, 6:31:34 PM1/2/08
to Google-We...@googlegroups.com
The normal way, I would guess, is to use the history token to set the view as in http://examples.roughian.com/#GWT_Panels
 

Ian
--------------------------------------------
http://examples.roughian.com
--------------------------------------------
 


 
--

Ian
--------------------------------------------
http://examples.roughian.com
http://examples.roughian.com/rxf/
--------------------------------------------

Tom Hjellming

unread,
Jan 2, 2008, 11:12:54 PM1/2/08
to Google-We...@googlegroups.com
I had a similar need and wrote the following JSNI method that is called
from my onModuleLoad(). It parses the parameters from the query string
within location.href:

private native String getPageParameter(String parm)
/*-{
var searchParm = parm + "=";
var url = $wnd.location.href;
var index = url.indexOf("?");
if (index > 0)
{
var queryString = url.substring(index+1, url.length);
var parms = queryString.split("&");
for (var i = 0; i < parms.length; i++)
{
if (parms[i].indexOf(searchParm) == 0)
{
// Found the id parm.
return parms[i].substring(searchParm.length,
parms[i].length);
}
}
}
return null;
}-*/;

Tom

Ian Bambury

unread,
Jan 3, 2008, 1:24:10 AM1/3/08
to Google-We...@googlegroups.com
Since I want to keep JSNI to a minimum, I wrote this:
 
class QueryParameters
{
 HashMap map = new HashMap();
 public QueryParameters()
 {
  String search = getQueryString();
  if(search.equals(""))return;
  ArrayList pairs = new ArrayList(Arrays.asList(search.substring(1).split("&")));
  for (Iterator i = pairs.iterator(); i.hasNext();)
  {
   String[] pair = ((String) i.next()).split("=");
   map.put(pair[0], pair[1]);
  }
 }
 public String getValue(String key)
 {
  return (String) map.get(key);
 }
 private native String getQueryString()
 /*-{
   return $wnd.location.search;
 }-*/;
}

which you can use like this:

  String file = parms.getValue("file");

You get a null if it doesn't exist

Ian Bambury

unread,
Jan 3, 2008, 1:25:51 AM1/3/08
to Google-We...@googlegroups.com
Sorry, you'll also need this to use it:
 

QueryParameters parms = new QueryParameters();

Tom Hjellming

unread,
Jan 3, 2008, 3:06:14 AM1/3/08
to Google-We...@googlegroups.com
Nice improvements Ian - I agree with keeping JSNI to a minimum. 

The copying of the array contents bothered me though, so I changed it to be:

public class QueryParameters
{
    private Map map = new HashMap();


    public QueryParameters()
    {
        String search = getQueryString();
        if ((search != null) && (search.length() > 0))
        {
            String[] nameValues = search.substring(1).split("&");
            for (int i = 0; i < nameValues.length; i++)
            {
                String[] pair = nameValues[i].split("=");

                map.put(pair[0], pair[1]);
            }
        }
    }

    public String getValue(String key)
    {
        return (String) map.get(key);
    }

    private native String getQueryString()
    /*-{
          return $wnd.location.search;
    }-*/;
}

Tom

Robert Hanson

unread,
Jan 3, 2008, 8:51:52 AM1/3/08
to Google-We...@googlegroups.com
The GWT Widget Library has support for this, http://gwt-widget.sourceforge.net.

Location loc = WindowUtils.getLocation();
String cust = loc.getParameter("customer");
String mon = loc.getParameter("month");

Rob
http://roberthanson.name

Reply all
Reply to author
Forward
0 new messages