You use the <flashvar> child tag to pass user-defined variables into a Flex application. The tag takes two attributes, name and value, and can be a child tag of either the <flash> or <mxml> tags.
The following example passes in the properties firstname and lastname to the test.swf file:
<mm:flash source="test.swf"> <mm:flashvar name="firstname" value="Nick" /> <mm:flashvar name="lastname" value="Danger" /> </mm:flash>
Flex converts <flashvar> variables into parameters for the <object> and <embed> tags in the resulting HTML wrapper. Using the previous example, Flex renders the following HTML wrapper:
<object codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/
swflash.cab#version=7,0,14,0 "classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="700" height="300"> <param name="movie" value="/dev/demos/test.swf"> <param name='flashVars' value='firstname=Nick&lastname=Danger'> <embed type="application/x-shockwave-flash "src="/dev/demos/test.swf "pluginspage="http://www.macromedia.com/go/getflashplayer" width="700" height="300" movie="/dev/demos/test.swf" flashVars='firstname=Nick&lastname=Danger'> </embed> </object>
When you pass a value into a Flex application with a <flashvar> tag, you can then use the value of that variable in the Flex application, as long as the variable is declared inside an MXML script block.
To use the variable in your Flex application, declare the variable name but do not initialize it. Flex can then access the value in its global scope. The following JSP fragment sets the value of a userID in the body of the JSP, declares the variable name in the Flex application, and then uses it in a function:
<%@ taglib uri="FlexTagLib" prefix="mm" %>
<% String userID = "2405"; %>
<mm:mxml>
<mm:flashvar name="userID" value="<%= userID %>" />
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml">
<mx:Script>
var userID:String;
function getUserID() {
vb1.label = vb1.label + userID;
}
</mx:Script>
<mx:Accordion>
<mx:VBox id="vb1" label="Panel 1: ">
<mx:Button label="Get User ID" click="getUserID();" />
</mx:VBox>
</mx:Accordion>
</mx:Application>
</mm:mxml>
The value of a <flashvar> tag does not have to be static. It can be any JSP expression that can be evaluated to a String, as the following example shows:
<mm:flashvar name="userId" value="<%= x.toString(); %>" />
You can bind the value of a parameter using the curly braces ({ }) shorthand, just as you would use curly braces in other instances. The following example prints the value that a parameter passes in a <flashvar> tag in the label:
<%@ taglib uri="FlexTagLib" prefix="mm" %>
<html><body>
<%
session.setAttribute("username", "nick");
String s = (String) session.getAttribute("username");
%>
<h3>Introduction</h3>
<p>This is an example of passing variables to an MXML application in a JSP.</p>
<h3>My App 2</h3>
<mm:mxml border="1">
<mm:flashvar name="param1" value="<%=s%>" />
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml">
<mx:Script>
var param1;
</mx:Script>
<mx:Label text="Hello {param1}" />
</mx:Application>
</mm:mxml>
</body></html>
Since the value of <flashvar> is not part of the MXML fragment itself, Flex does not recompile the application when the value changes (although the application server may recompile the JSP page). This is a more efficient way of passing dynamic data to the Flex application than mixing JSP expressions inside the MXML code, but it restricts you to String values of a relatively short length. Also, your MXML code must declare a variable for each passed <flashvar> that you access inside the application.
In addition to passing variables into Flex applications with the <flashvar> tag, you can append them as query string parameters. Flex converts query string parameters to flashVars in the HTML wrapper. For more information, see Passing request data to Flex applications.
Version 1.5
Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/flex/15/flex_docs_en/00002253.htm