Avg. Rating 3.4

Problem

You want to pass on data from the wrapper to the Flex SWF but this data is dynamically created or passed by Java.

Solution

Use the <%=%> JSP syntax in the wrapper to dynamically write a FlashVar.

Detailed explanation

You want to retrieve data, e.g the username, from the wrapper in order to be able to request other information from the server afterwards.

The way to do this is to dynamically write the information in a FlashVar in the wrapper. At startup, the Flex SWF will read this information.

You can create a JSP by saving main.html as e.g main.jsp at the root of your project.

E.g, in a JSP wrapper:
            
Locate the AC_FL_RunContent Javascript method and add a variable using this syntax: &var=<%=value%>. There are already some variables there: MMredirectURL, MMplayerType and MMdoctitle. We add the username that the user entered to login with Java (JAAS).
        
        AC_FL_RunContent(
        "src", "playerProductInstall",
        "FlashVars", "MMredirectURL="+MMredirectURL+'&MMplayerType='+MMPlayerType+
'&username=<%=request.getUserPrincipal().getName()%>&MMdoctitle='+MMdoctitle+"",
        "width", "100%",
        "height", "100%",
        "align", "middle",
        "id", "main",
        "quality", "high",
        "bgcolor", "#ffffff",
        "name", "main",
        "allowScriptAccess","sameDomain",
        "allowFullScreen","true",
        "type", "application/x-shockwave-flash",
        "pluginspage", "http://www.adobe.com/go/getflashplayer"
    );    

Be sure to add your variable in both the (hasProductInstall && !hasRequestedVersion) and (hasRequestedVersion) sections as well as in the <object> and <embed> tags (for users who disabled Javascript in the browser):
        
    <noscript>
          <object id="main"
                  classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
                width="100%"
                height="100%"
                codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">
                
                <param name="movie" value="main.swf" />
                <param name="quality" value="high" />
                <param name="bgcolor" value="#ffffff" />
                <param name="allowScriptAccess" value="sameDomain" />
                <param name="allowFullScreen" value="true" />
                <param name="FlashVars" value="username=<%=request.getUserPrincipal().getName()%>" />
                
                <embed
                    src="main.swf"
                    quality="high"
                    bgcolor="#ffffff"
                    width="100%"
                    height="100%"
                    name="main"
                    align="middle"
                    play="true"
                    loop="false"
                    quality="high"
                    allowScriptAccess="sameDomain"
                    allowFullScreen="true"               FlashVars="username=<%=request.getUserPrincipal().getName()%>"
                    type="application/x-shockwave-flash"
                    pluginspage="http://www.adobe.com/go/getflashplayer">
                </embed>
        </object>
    </noscript>
    
    In Flex:
    
        In main.mxml, in the applicationComplete handler:

<mx:Application
    xmlns:mx="http://www.adobe.com/2006/mxml"
    applicationComplete="onApplicationComplete()">
  
            private function onApplicationComplete():void
            {
                // Get the username from the wrapper FlashVar
                this.model.user.userId = this.parameters.username;
            }
   
</mx:Application>

In the Run () section of Eclipse, change the path from main.html to main.jsp so you can run the application using the new wrapper:


Report abuse

Related recipes