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.
我有一个Google Web Toolkit(GWT)应用程序,当我链接到它时,我想传递一些可用于动态检索数据的参数/参数。例如。如果它是股票图表应用程序,我希望我的链接包含该符号,然后让GWT应用程序读取并向某些股票服务发出请求。例如。 http:// myapp / gwt / StockChart?symbol = GOOG将是我的StockChart GWT应用程序的链接,它会向我的股票信息网站服务请求GOOG股票。
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).
到目前为止,我一直在使用服务器端代码将Javascript变量添加到页面,然后我使用JSNI(JavaScript Native Interface)读取这些变量。
For example:
In the host HTML:
在主机HTML中:
<script type="text/javascript">
var stockSymbol = '<%= request.getParameter("symbol") %>';
</script>
In the GWT code:
在GWT代码中:
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.
但是,这在托管模式下并不总是有效(特别是对于数组),并且由于JSNI在版本1.4和之前的版本中并不存在,我猜测还有另一种/更好的方法。
2 个解决方案
#1
10
If you want to read query string parameters from the request you can use the com.google.gwt.user.client.Window class:
如果要从请求中读取查询字符串参数,可以使用com.google.gwt.user.client.Window类:
// 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);
}
#2
1
It is also a nice option to 'parameterize' a GWT application using hash values.
使用哈希值“参数化”GWT应用程序也是一个不错的选择。
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.
通过GWT的历史机制为这些“参数”提供了一些很好的工具支持。
#1
10
If you want to read query string parameters from the request you can use the com.google.gwt.user.client.Window class:
如果要从请求中读取查询字符串参数,可以使用com.google.gwt.user.client.Window类:
// 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);
}
#2
1
It is also a nice option to 'parameterize' a GWT application using hash values.
使用哈希值“参数化”GWT应用程序也是一个不错的选择。
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.
通过GWT的历史机制为这些“参数”提供了一些很好的工具支持。