0

我在应用引擎中使用 python 和 webapp 框架作为后端,使用 flex 4 作为前端。我想将一个字符串形式的后端传递给前端,所以我在 main.py 中编写了以下代码:

class MainPage(webapp.RequestHandler):

 def get(self):

  userVO = "test"

  template_values = {
   'url': self.request.uri,
   'userVO': userVO,

  }
  self.response.out.write(template.render("templates/index.html", template_values))

在 flex 4 中,我有以下代码:

var user:String = FlexGlobals.topLevelApplication.parameters['userVO'];

但是,我收到空值。

请建议如何纠正它。谢谢。

编辑:2月25日

感谢回答我问题的人。对于我的问题,我试图弄清楚 python 应用程序引擎在呈现包含 swf 文件的 html 文件时如何将数据传递给 flex 应用程序。也许,我可以在 main.py、swfobject.js 或 index.html 中设置一些东西来完成我的任务。

我知道如何使用 Pyamf 作为服务 flex 应用程序的网关,我正在考虑如何使应用程序更简单。

谢谢。

编辑:2月28日

罗伯特,index.html 是由 flash builder 4 创建的标准文件。希望你能给我一些如何修改它的提示。以下是文件:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- saved from url=(0014)about:internet -->  

<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> 
<!-- 
Smart developers always View Source. 

This application was built using Adobe Flex, an open source framework
for building rich Internet applications that get delivered via the
Flash Player or to desktops via Adobe AIR. 

Learn more about Flex at http://flex.org 
// -->
   <head>
   <title></title>         
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

html, 身体 { 高度:100%; } 身体 { 边距:0; 填充:0;溢出:隐藏;文本对齐:居中;}
#flashContent { 显示:无;}

    <script type="text/javascript" src="/js/swfobject.js"></script>
    <script type="text/javascript">
        <!-- For version detection, set to min. required Flash Player version, or 0 (or 0.0.0), for no version detection. --> 
        var swfVersionStr = "10.0.0";
        <!-- To use express install, set to playerProductInstall.swf, otherwise the empty string. -->
        var xiSwfUrlStr = "/swfs/playerProductInstall.swf";
        var flashvars = {};
        var params = {};
        params.quality = "high";
        params.bgcolor = "#ffffff";
        params.allowscriptaccess = "sameDomain";
        var attributes = {};
        attributes.id = "index";
        attributes.name = "index";
        attributes.align = "middle";
        swfobject.embedSWF(
            "/swfs/index.swf", "flashContent", 
            "100%", "100%", 
            swfVersionStr, xiSwfUrlStr, 
            flashvars, params, attributes);

swfobject.createCSS("#flashContent", "display:block;text-align:left;");

要查看此页面,请确保安装了 Adob​​e Flash Player 10.0.0 或更高版本。

获取 Adob​​e Flash 播放器

    <noscript>
        <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="100%" height="100%" id="index">
            <param name="movie" value="index.swf" />
            <param name="quality" value="high" />
            <param name="bgcolor" value="#ffffff" />
            <param name="allowScriptAccess" value="sameDomain" />
            <!--[if !IE]>
            <object type="application/x-shockwave-flash" data="index.swf" width="100%" height="100%">
                <param name="quality" value="high" />
                <param name="bgcolor" value="#ffffff" />
                <param name="allowScriptAccess" value="sameDomain" />
            <![endif]-->
            <!--[if gte IE 6]>
             <p> 
              Either scripts and active content are not permitted to run or Adobe Flash Player version
              10.0.0 or greater is not installed.
             </p>
            <![endif]-->
                <a href="http://www.adobe.com/go/getflashplayer">
                    <img src="https://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash Player" />
                </a>
            <!--[if !IE]>
            </object>
            <![endif]-->
        </object>
 </noscript>  

谢谢,罗伯特。

编辑:3月7日。

罗伯特,

参考http://help.adobe.com/en_US/Flex/4.0/html/WS2db454920e96a9e51e63e3d11c0bf626ae-7feb.html

在我在这里发布问题之前,我尝试了以下代码:

在 index.html 中,

<%
     String user = (String) request.getParameter("userVO");
%>

并且

flashvars.userVO =  "<%= user %>"; 

结果,我得到:

<用户

你知道为什么我不能得到正确的数据吗?谢谢。

4

3 回答 3

2

从 Flex 到 GAE 的最佳交流方式是使用 AMF。方法如下:

应用程序.yaml

application: flexandthecloud
version: 3
runtime: python
api_version: 1

handlers:
- url: /services/.*
  script: main.py

主文件

#!/usr/bin/env python
import wsgiref.handlers

from pyamf.remoting.gateway.wsgi import WSGIGateway

def sayHello(name):
  return "howdy " + name

services = {
    'services.sayHello': sayHello
}

def main():
  application = WSGIGateway(services)
  wsgiref.handlers.CGIHandler().run(application)

if __name__ == '__main__':
  main()

Flex 3 代码(可以为 Flex 4 轻松修改):

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">

    <mx:RemoteObject id="ro" destination="services" endpoint="http://flexandthecloud.appspot.com/services/">
        <mx:result>
            l.text = event.result as String;
        </mx:result>
    </mx:RemoteObject>

    <mx:TextInput id="ti"/>
    <mx:Label id="l"/>
    <mx:Button label="say hello" click="ro.sayHello(ti.text)"/>

</mx:Application>
于 2010-02-22T15:45:59.010 回答
1

你的 index.html 中有什么?您可以将值传递给 index.html,设置一个 javascript 函数,如:

function passvalue {
    PassParameter("userVO", "{{userVO}}");
}

然后在 Flex 中设置一个函数:

public function gethtmlparam(name:String, val:String):void {
            switch (name) {
                case "userVO": userVO= val; break;}
}

并在以下位置回调这两个函数:

ExternalInterface.addCallback("PassParameter", gethtmlparam);

希望它可以提供帮助。

于 2010-02-25T00:09:10.087 回答
1

根据您对 James Ward 回复的评论,我想知道您是否可以使用 FlashVars 参数元素完成您需要的工作?

http://livedocs.adobe.com/flex/3/html/help.html?content=passingarguments_3.html

您可能只需要调整 index.html 模板来构建 FlashVars 参数元素。

编辑:3 月 6 日

您可以尝试查看: http: //polygeek.com/801_flex_reading-flashvars-in-flex

您需要确保等到应用程序加载完毕才能访问 flashVars。

编辑:3 月 7 日

正确,在这些示例中,他们对值进行了硬编码。您需要编辑模板,以便将值设置为模板参数的值。

因此,在 index.html 中: flashvars.userVO = "{{ userVO }}"

于 2010-02-26T16:20:31.800 回答