如何将数据从Google App Engine(Python)传递到Flex 4应用程序
我正在使用Python和Web应用框架在应用引擎上做后端,前端用的是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, body { height:100%; }
body { margin:0; padding:0; overflow:hidden; text-align:center; }
#flashContent { display:none; }
<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;");
要查看此页面,请确保安装了Adobe Flash Player版本10.0.0或更高版本。

<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 %>";
结果是,我得到了:
< user
你知道我为什么不能得到正确的数据吗?谢谢。
3 个回答
你的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);
希望这些能对你有帮助。
从Flex与GAE(Google App Engine)之间进行交流,最好的方式是使用AMF。下面是具体的做法:
首先是配置文件app.yaml:
application: flexandthecloud
version: 3
runtime: python
api_version: 1
handlers:
- url: /services/.*
script: main.py
接下来是主程序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>
根据你对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 }}"