将参数传递给GWT bootstrap.nocache.js脚本
有没有办法将参数传递给GWT生成的.nocache.js脚本文件,并在onModuleLoad函数中对其求值?像这样:将参数传递给GWT bootstrap.nocache.js脚本,gwt,parameters,Gwt,Parameters,有没有办法将参数传递给GWT生成的.nocache.js脚本文件,并在onModuleLoad函数中对其求值?像这样: <script type="text/javascript" src="application/Application.nocache.js?appId=461333815262909"></script> 主机页URL应该与内部工作的GWT内容完全分离,因此不能将appId参数作为主机页的查询参数传递并使用Window.Location.getP
<script type="text/javascript" src="application/Application.nocache.js?appId=461333815262909"></script>
主机页URL应该与内部工作的GWT内容完全分离,因此不能将appId参数作为主机页的查询参数传递并使用Window.Location.getParameter访问它。我知道我可以隐藏这些参数,例如隐藏在隐藏的div中,然后从脚本中查询它们,但是如果可能的话,我希望避免在主机页中进一步依赖它们
谢谢!
Lisa没有,但可能有助于将参数从服务器传递到客户端脚本,以便在页面加载时进行评估。GWT中似乎没有对此的本机支持,但我最近提出了以下解决方案: 假设脚本始终遵循命名约定“
/.nocache.js
”,则可以从主机页获取所有
元素,并在src
属性中搜索引用该元素的元素。然后可以从那里提取URL编码的属性
这是我的示例实现,打算使用GWT.getModuleName()
作为第一个参数调用
/**
* Fetches a parameter passed to the module's nocache script.
*
* @param moduleName the module's name.
* @param parameterName the name of the parameter to fetch.
* @return the value of the parameter, or <code>null</code> if it was not
* found.
*/
public static native String getParameter( String moduleName, String parameterName ) /*-{
var search = "/" + moduleName + ".nocache.js";
var scripts = $doc.getElementsByTagName( "script" );
for( var i = 0; i < scripts.length; ++i ) {
if( scripts[ i ].src != null && scripts[ i ].src.indexOf( search ) != -1 ) {
var parameters = scripts[ i ].src.match(/\w+=\w+/g);
for( var j = 0; j < parameters.length; ++j ) {
var keyvalue = parameters[ j ].split( "=" );
if( keyvalue.length == 2 && keyvalue[ 0 ] == parameterName ) {
return unescape( keyvalue[ 1 ] );
}
}
}
}
return null;
}-*/;
欢迎提出改进建议。传递参数的简单方法是通过HTML元标记,而不是将信息隐藏在可能变得混乱的隐藏div中 在调用GWT脚本的HTML页面中,添加一个元标记,如下所示:
<html>
<head>
<meta name="appId" content="461333815262909">
...
@Override
public void onModuleLoad() {
NodeList<Element> metas = Document.get().getElementsByTagName("meta");
for (int i=0; i<metas.getLength(); i++) {
MetaElement meta = (MetaElement) metas.getItem(i);
if ("appId".equals(meta.getName())) {
Window.alert("Module loaded with appId: " + meta.getContent());
}
}
}
...
然后,在模块的入口点中,按如下方式对其进行解析:
<html>
<head>
<meta name="appId" content="461333815262909">
...
@Override
public void onModuleLoad() {
NodeList<Element> metas = Document.get().getElementsByTagName("meta");
for (int i=0; i<metas.getLength(); i++) {
MetaElement meta = (MetaElement) metas.getItem(i);
if ("appId".equals(meta.getName())) {
Window.alert("Module loaded with appId: " + meta.getContent());
}
}
}
@覆盖
moduleload()上的公共void{
NodeList metas=Document.get().getElementsByTagName(“meta”);
对于(int i=0;i