Video | Code | Tutorial | Links
Note: Flex Test Drive requires Flex 4.5 SDK and Flash Builder 4.5; download a 60-day trial version of Flash Builder 4.5 before continuing if you haven't already.
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
<s:Label x="50" y="50" color="maroon" fontSize="20" fontWeight="bold" text="XYZ Corporation Directory"/>
<s:Button id="empBtn" x="50" y="100" label="Employees"/>
<s:Button id="deptBtn" x="140" y="100" label="Departments"/>
<s:DataGrid id="empDg" x="50" y="130" requestedRowCount="4">
<s:columns>
<s:ArrayList>
<s:GridColumn dataField="dataField1" headerText="Column 1"></s:GridColumn>
<s:GridColumn dataField="dataField2" headerText="Column 2"></s:GridColumn>
<s:GridColumn dataField="dataField3" headerText="Column 3"></s:GridColumn>
</s:ArrayList>
</s:columns>
<s:typicalItem>
<fx:Object dataField1="Sample Data" dataField2="Sample Data" dataField3="Sample Data"></fx:Object>
</s:typicalItem>
<s:ArrayList>
<fx:Object dataField1="data1" dataField2="data1" dataField3="data1"></fx:Object>
<fx:Object dataField1="data2" dataField2="data2" dataField3="data2"></fx:Object>
<fx:Object dataField1="data3" dataField2="data3" dataField3="data3"></fx:Object>
<fx:Object dataField1="data4" dataField2="data4" dataField3="data4"></fx:Object>
</s:ArrayList>
</s:DataGrid>
</s:Application>
In this Test Drive, you are going to create a Flex application that retrieves, displays, and modifies database records (see Figure 1). A Flex application does not connect directly to a remote database. Instead, you connect it to a data service written in your favorite web language (PHP, ColdFusion, Java, or any other server-side web technology). You will build the front-end Flex application; the database and the server-side code to read, add, edit, and delete database records is provided for you as a PHP class, a ColdFusion component, or Java classes.
In this module, you are going to build a Flex project and a Flex application that retrieves data from the database and displays it. Employee data will be displayed on one "page" in the application and Department data on another.
The first task is to create a new Flex project for your application server and create the user interface. You'll retrieve data from the server and display it in the next tutorial.
Follow the instructions in one of the sections below.
The setup files include a database and server-side files to manipulate data in the database. Your Flex application will call methods of one of these server-side files, EmployeeService.
Note: If you installed the server files for the Flex 4.5 Test Drive for Mobile (add link) or the Flex 4.5 trial email sample applications, you can skip this step. The same server-side files are used for all three.
username, password, server, port, and databasename properties to the correct values for your server setup. This class file contains the methods you will call from your Flex application.<property-case> tag and change all three values to true as shown below:<property-case>
<!-- cfc property names -->
<force-cfc-lowercase>true</force-cfc-lowercase>
<!-- Query column names -->
<force-query-lowercase>true</force-query-lowercase>
<!-- struct keys -->
<force-struct-lowercase>true</force-struct-lowercase>
</property-case>
Note: If you are using an earlier version of ColdFusion, your configuration file may not have these tags and you will need to add them. For details, refer to the documentation on using Flash Remoting with your particular server.
http://{your server:your port}/TestDrive/test/test.html
For a default standalone ColdFusion installation, browse to:
http://localhost:8500/TestDrive/test/test.html
You should see employee data from the database successfully displayed in a datagrid.
Note: For Windows, the ConnectionHelper.java file assumes the testdrive web app is deployed on the C drive. If it is not deployed on the C drive, you need to modify this file appropriately and recompile it, replacing /{your server webapps folder}/testdrive/WEB-INF/classes/services/ConnectionHelper.class.
http://{your server:your port}/testdrive/test/test.html
For example, for the BlazeDS turnkey server, browse to:
http://localhost:8400/testdrive/test/test.html
You should see employee data from the database successfully displayed in a datagrid.
Follow the instructions in one of the sections below.
Use the Package Explorer to explore the generated project(s) and files.
When you create a new Flex project, Flash Builder creates an MXML file with the same name as the project (see Figure 7). This is the main application file where you add your code. You create Flex applications using two languages: ActionScript and MXML. Typically, you use MXML and Flex components to create application interfaces and ActionScript and events to program application logic. MXML tags and ActionScript code can reference each other, similar to HTML tags and JavaScript code.
In FlexWebTestDrive.mxml, the first line of code is the XML declaration for the parser. The next line is the <s:Application> tag, which defines the Application container that must be the root tag for a Flex application. When the application is compiled, a SWF file, an HTML wrapper page that references the SWF file, and other files are created and placed in the bin-debug folder on your application server so you can browse to the application. When you browse to the HTML file that references the application SWF, the SWF file is downloaded and rendered by Flash Player (available as a browser plugin or ActiveX control).
If you created a Flex and PHP project with Flash Builder 4.5 for PHP, you will also have a PHP project (see Figure 8) for viewing and modifying your PHP code.
Switch to Design mode and drag out Label, DataGrid, and Button components from the Components view to create the interface shown in Figure 9. Use the Properties view to assign component ids of empBtn, deptBtn, and empDg and set other properties.
HTML applications are built from document elements such as headings, paragraphs, and tables. Flex applications are built from components such as Buttons, CheckBoxes, and DataGrids. The Flex 4.5 framework includes over 100 components, including user interface controls to display content and provide user interaction and containers to manage the layout.
Switch to Source mode and take a look at the generated code. You will see a new tag for each of the components you added. The order of the tags does not matter because the Application container uses absolute positioning by default so component positions are set by their x and y properties.
The <s:Label> tag represents a Label control, a very simple user interface component that displays text. Its text property is set to "XYZ Corporation Directory" and its x and y properties are set to where it will appear in the interface. The color, fontSize, and , and fontWeight attributes change the style of the font used. In Flex, you can set color styles to a string for any of the 16 colors in the VGA color set (for example, red or blue) or an RGB triplet in hexadecimal format (for example, #FF0000 or #0000FF).
<s:Label x="50" y="50" text="XYZ
Corporation Directory" color="maroon"
fontSize="20" fontWeight="bold"/>
For each component, you can specify properties and styles. Properties apply only to that particular component instance. Styles can be set inline as done here or using CSS to create style rules to apply to your components.
Use the Flash Builder Content Assist to select and set values for various properties and styles.
When you place your cursor inside a tag and press the spacebar or Ctrl+spacebar, you get code hinting with the Flash Builder Content Assist (see Figure 10). It shows a list of all the attributes you can set for that tag, including properties, styles, events, and more. Different symbols are used for each different type of attribute. This is the same list you see in the Alphabetical view of the Design mode Properties view.
You can get more complete descriptions for each of the attributes in the component's API, its application programming interface. To navigate to a component's API, select Help > Dynamic Help and then click a component tag in your code. You will see a link to that component's API in the Help view, which you click to open the API (see Figure 11).
Use the Run button or the Run menu to compile the application and view it in an HTML page in a browser window.
Your application appears in the browser inside a generated HTML wrapper page (see Figure 12).
PHP developers using Flash Builder for PHP: The first time you run an application, you get a Run As dialog box. If you select Web Application, an HTML wrapper file is run. If you select Web (PHP) Application, a PHP wrapper file is run. The code for both files is the same so you can choose either option. If you want to debug your PHP service code when running this application (debugging is covered in Debug client-side code), you need to launch a PHP wrapper page, so choose Web (PHP) application. You can change which file is run at any time by selecting Run > Run As > Web (PHP) Application or Web Application or creating run configurations.
You will retrieve and display data in the DataGrid in the next tutorial and add application "pages" and wire up the buttons later.
Refer to the following resources to learn more about this topic:
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.