【问题标题】:Pass parameter to GWT nocache.js from external web portal从外部门户网站将参数传递给 GWT nocache.js
【发布时间】:2012-11-03 05:48:04
【问题描述】:

我创建了一个GWT 小部件,其根容器 ID 为“widgetContainer”,对应的编译文件为 gwtwidget.nocache.js。我已经创建了这个小部件的 war 文件并托管在本地服务器 localhost:8888/gwtwidget 中。

我创建了另一个jsp应用,index.jsp如下:(将GWT模块集成到JSP应用中)

<html>
<head>
    <script type="text/javascript" language="javascript" src="http://localhost:8888/gwtwidget/gwtwidget/gwtwidget.nocache.js?appId=461333815262909&appId=461333815262909&appId=461333815262909&appId=461333815262909"></script>
</head>
<body>
    <div id="widgetContainer"></div>
</body>
</html>

我想从JSP 应用程序中检索通过 nocache.js 文件传递​​的参数。为此,我正在使用代码

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;
}-*/;   

但无法将参数值获取到GWT 小部件中。谁能帮我解决这个问题?

提前致谢。

【问题讨论】:

    标签: javascript gwt methods native


    【解决方案1】:

    如果您想使用查询字符串传递参数,这并不简单。有办法做到这一点。您可以参考以下帖子:

    1. Passing parameters to JavaScript files
    2. Pass vars to JavaScript via the SRC attribute

    还有一种方法,我觉得比较简单。您可以在 jsp 文件中设置 cookie。您可以在 GWT 模块中读取 cookie。

    <html>
    <head>
    <script type="text/javascript">
        document.cookie="appId=461333815262909"
    </script>
        <script type="text/javascript" language="javascript" src="http://localhost:8888/gwtwidget/gwtwidget/gwtwidget.nocache.js"></script>
    </head>
    <body>
        <div id="widgetContainer"></div>
    </body>
    </html>
    

    您可以通过以下方式访问 GWT 模块中的 cookie 'appId':

    String param = Cookies.getCookie("appId");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-04
      • 2014-08-20
      相关资源
      最近更新 更多