Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Download It


Published on

  • Be the first to comment

  • Be the first to like this

Download It

  1. 1. Java and Flex
  2. 2. Mysql-java-flex with a flex server <ul><li>You’ll need a flex server: </li></ul><ul><li>Fds-tomcat has been pre-configured to run flex apps. </li></ul><ul><li>Regular tomcat can run flex too </li></ul><ul><li>JRun can be downloaded preconfigured with LCDS (Flex life-cycle data services express edition) </li></ul><ul><li>You’ll also need Hibernate, mysql & java </li></ul><ul><li>Hibernate is open source and comes as a jar file that needs to be in the classpath. </li></ul><ul><li> </li></ul><ul><li>This tutorial was relatively easy to do using JRun . </li></ul>
  3. 3. Install LDS (adobe livecycle data services) with integrated JRun4.. <ul><li>This contains the same LCDS as the fds-tomcat and has a similar bunch of samples. </li></ul><ul><li>JRun comes preconfigured. </li></ul><ul><li>This is the version I used although the next screen shot is of the fancier version </li></ul>
  4. 4. JRun4 – the full-blown version shown below - needs to be configured for LCDS but allows you to add servers as needed.
  5. 5. This tutorial (approximately the next 14 slides) <ul><li>You’ll need to comment out the java.home definition in the jvm.config.xml file or you’ll get a version error between your compiler and the jvm packaged with the LCDS. </li></ul><ul><li>The main.mxml works fine but you’ll need to do some work to get the popup window working. </li></ul>
  6. 6. Create a mysql database and a table in it named employee…use this script if you like <ul><li>CREATE DATABASE SampleDB; USE SampleDB; </li></ul><ul><li>DROP TABLE IF EXISTS `employee`; CREATE TABLE `employee` (   `employeeid` int(10) unsigned NOT NULL auto_increment,   `firstname` varchar(255) default NULL,   `lastname` varchar(255) default NULL,   PRIMARY KEY  (`employeeid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; </li></ul><ul><li>INSERT INTO `employee` (`employeeid`,`firstname`,`lastname`) VALUES  (1,'Conrad','Simms'),  (2,'Akira','Tanaka'),  (3,'Rye','Woodard'); </li></ul>
  7. 7. Create <ul><li>package EmployeeManagement;        public class Employee {      private int id;               private String firstname;               private String lastname;              public void setId(int id)             {            = id;           }               public int getId()          {                    return;              }                      public void setFirstname(String firstname)               {                     this.firstname = firstname;               }               public String getFirstname()               {                    return firstname;               }                 public void setLastname(String lastname)               {                 this.lastname = lastname;               }               public String getLastname()               {                     return lastname;               }       } </li></ul>
  8. 8. Save <ul><li>Save it in </li></ul><ul><li>Create directories as needed </li></ul><ul><li>Compile it and save the .class file in c:fdsjrun4serversdefaultflexWEB-INFclassesEmployeeManagement </li></ul>
  9. 9. Hibernate mapping file <ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><!DOCTYPE hibernate-mapping PUBLIC               &quot;-//Hibernate/Hibernate Mapping DTD 3.0//EN&quot;                      &quot;;>        <hibernate-mapping>            <class name=&quot;EmployeeManagement.Employee&quot; table=&quot;employee&quot;>                      <id name=&quot;id&quot; column=&quot;employeeid&quot;>                      <generator class=&quot;native&quot;/>                      </id>                      <property name=&quot;firstname&quot;/>                      <property name=&quot;lastname&quot;/>             </class>        <!-- This is a named query that we will use later -->           <query name=&quot;all.employees&quot;>From Employee</query> </hibernate-mapping> </li></ul><ul><li>       </li></ul><ul><li>Save this file as Employee.hbm.xml in the same folder where we just placed our persistent (java) class </li></ul>
  10. 10. Creating the Hibernate configuration file <ul><li>  This file contains the configuration settings that will enable Hibernate to connect to the database that we would like to access. The following Hibernate configuration contains the minimal settings required for this tutorial to work: </li></ul><ul><li><?xml version='1.0' encoding='utf-8'?> </li></ul><ul><li><!DOCTYPE hibernate-configuration PUBLIC         &quot;-//Hibernate/Hibernate Configuration DTD 3.0//EN&quot;         &quot;;> <hibernate-configuration>     <session-factory> </li></ul><ul><li>        <!-- Database connection settings -->         <property name=&quot;connection.driver_class&quot;>com.mysql.jdbc.Driver</property>         <property name=&quot;connection.url&quot;>jdbc:mysql://localhost/SampleDB</property>         <property name=&quot;connection.username&quot;>root</property>         <property name=&quot;connection.password&quot;></property> </li></ul><ul><li>        <!-- JDBC connection pool (use the built-in) -->         <property name=&quot;connection.pool_size&quot;>1</property> </li></ul><ul><li>        <!-- SQL dialect -->         <property name=&quot;dialect&quot;>org.hibernate.dialect.MySQLDialect</property> </li></ul><ul><li>        <!-- Enable Hibernate's automatic session context management -->         <property name=&quot;current_session_context_class&quot;>thread</property> </li></ul><ul><li>        <!-- Disable the second-level cache  -->         <property name=&quot;cache.provider_class&quot;>org.hibernate.cache.NoCacheProvider</property>         <!-- Echo all executed SQL to stdout -->         <property name=&quot;show_sql&quot;>true</property> </li></ul><ul><li>        <!-- Load the database table mapping file -->         <mapping resource=&quot;EmployeeManagement/Employee.hbm.xml&quot;/> </li></ul><ul><li>    </session-factory> </li></ul><ul><li></hibernate-configuration> </li></ul><ul><li>This file must be named hibernate.cfg.xml and must be in the application's classpath. Therefore, we will save this file in the following location: C:fds2jrun4serversdefaultflexWEB-INFclasses </li></ul>
  11. 11. edit the data-management-config.xml file located in C:fdsjrun4serversdefaultflexWEB-INFflex <ul><li><?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>        <service id=&quot;data-service&quot;               class=&quot;;               messageTypes=&quot;;>        <adapters>                      <adapter-definition id=&quot;actionscript&quot; class=&quot;; default=&quot;true&quot;/>               <adapter-definition id=&quot;java-dao&quot; class=&quot;;/>        </adapters> </li></ul><ul><li>       <default-channels>                      <channel ref=&quot;my-rtmp&quot;/>        </default-channels> </li></ul><ul><li>   <destination id=&quot;employee.hibernate&quot;>         <adapter ref=&quot;java-dao&quot; />         <properties>             <use-transactions>true</use-transactions>             <source></source>             <scope>application</scope>             <metadata>        <!--This is the unique identifier from the hibernate-entity bean -->                 <identity property=&quot;id&quot;/>             </metadata>             <network>                 <session-timeout>20</session-timeout>                 <paging enabled=&quot;false&quot; pageSize=&quot;10&quot; />                 <throttle-inbound policy=&quot;ERROR&quot; max-frequency=&quot;500&quot;/>                 <throttle-outbound policy=&quot;REPLACE&quot; max-frequency=&quot;500&quot;/>             </network>             <server>                 <hibernate-entity>EmployeeManagement.Employee</hibernate-entity>                 <fill-method>                     <name>fill</name>                     <params>java.util.List</params>                 </fill-method>                 <fill-configuration>                     <use-query-cache>false</use-query-cache>                     <allow-hql-queries>true</allow-hql-queries>                 </fill-configuration>             </server>         </properties>     </destination>    </service> </li></ul>
  12. 12. Adding dependent jars <ul><li>copy the mysql-connector-java-3.0.15-ga-bin.jar to </li></ul><ul><li>C:fdsjrun4serversdefaultflexWEB-INFlib </li></ul><ul><li>From the Flex Data Services, navigate to the resources/hibernate folder, copy all of the jars from that folder to the same location as the MySQL driver you just copied. </li></ul>
  13. 13. an actionscript file <ul><li>package EmployeeManagement        {               [Managed]               [RemoteClass(alias=&quot;EmployeeManagement.Employee&quot;)]               public class Employee               {                      public function Employee() {}                                           public var id:int;                      public var firstname:String=&quot;&quot;;                      public var lastname:String=&quot;&quot;;               }        } </li></ul><ul><li>Save this file as in : </li></ul><ul><li>C:fds2jrun4serversdefaultflexEmployeeManagerEmployeeManagement </li></ul>
  14. 14. The flex application <ul><li><?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?> <mx:Application xmlns:mx=&quot;; layout=&quot;absolute&quot; creationComplete=&quot;getEmployees()&quot;> <mx:Script>               <![CDATA[                      import mx.controls.Alert;                      import;                      import mx.collections.ArrayCollection;                      import;                      import mx.rpc.AsyncToken;                      import*;                      import EmployeeManagement.*;                                                      [Bindable]                      private var employee:Employee;                                           private function getEmployees():void {                             currentState='';                             hibernate.fill(myArray,&quot;all.employees&quot;,[]);                      }                                           private function handleFault(event:FaultEvent):void {                   ;                                                  }               ]]>        </mx:Script>        <mx:DataService id=&quot;hibernate&quot; destination=&quot;employee.hibernate&quot; fault=&quot;handleFault(event)&quot; autoCommit=&quot;true&quot; />        <mx:ArrayCollection id=&quot;myArray&quot; /> <mx:VBox width=&quot;783&quot; height=&quot;562&quot; y=&quot;10&quot; x=&quot;10&quot;> <mx:ApplicationControlBar width=&quot;782&quot; borderColor=&quot;#408080&quot; fillColors=&quot;[#408080, #408080]&quot; fillAlphas=&quot;[0.53, 0.53]&quot; autoLayout=&quot;true&quot;> <mx:Text text=&quot;Employee Management Console&quot; fontSize=&quot;26&quot; fontWeight=&quot;bold&quot; fontStyle=&quot;italic&quot; themeColor=&quot;#ff8000&quot; alpha=&quot;0.52&quot;/> </mx:ApplicationControlBar> <mx:HBox x=&quot;10&quot; y=&quot;10&quot; width=&quot;783&quot; height=&quot;455&quot; id=&quot;hbox1&quot;> <mx:Panel  id=&quot;listEmployeePanel&quot; width=&quot;783&quot; height=&quot;455&quot; layout=&quot;absolute&quot; title=&quot;Employee List&quot; cornerRadius=&quot;8&quot; borderColor=&quot;#408080&quot; fontSize=&quot;13&quot;>        <mx:DataGrid id=&quot;dgrid&quot; dataProvider=&quot;{myArray}&quot; editable=&quot;false&quot; width=&quot;763&quot; height=&quot;411&quot; bottom=&quot;0&quot; right=&quot;0&quot;>               <mx:columns>                      <mx:DataGridColumn headerText=&quot;Employee ID&quot; dataField=&quot;id&quot;/>                      <mx:DataGridColumn headerText=&quot;First Name&quot; dataField=&quot;firstname&quot;/>                      <mx:DataGridColumn headerText=&quot;Last Name&quot; dataField=&quot;lastname&quot;/>               </mx:columns>        </mx:DataGrid> </mx:Panel> </mx:HBox>        </mx:VBox> </li></ul><ul><li></mx:Application> </li></ul>
  15. 15. Pop up window <ul><li>In my flex.ppt (and on the adobe help page) you can find information on setting up a pop up window. </li></ul><ul><li>The code for a pop up window for this application to create a new employee is in this slide’s notes and accompanies the download files. </li></ul>
  16. 16. The application loads hibernate from mysql and uses a simple java bean and flex… here it is running on JRun4
  17. 17. Pop up window to add new employee
  18. 18. Mysql table shows new employees added
  19. 19. Multiple web apps <ul><li>One way to establish multiple web apps running on the server is to create multiple directories under lcds/jrun4/servers/default. </li></ul><ul><li>Create a subdirectory employee (or something) at this level and copy necessary files over, maintaining the directory structure. </li></ul>
  20. 20. Note url
  21. 21. About Java DAO and Flex <ul><li>Coenraets’ fds-tomcat contains an entire tomcat distribution configured to run flex. </li></ul><ul><li>Download it at: 30 minute test drive </li></ul><ul><li>This “test drive” also contains tutorials and examples showing flex use with hibernate, spring and java. </li></ul><ul><li>His pdf on java dao/flex (Flex Data Management Services Tutorial link in the index) is a good starting point tutorial to get your feet wet. </li></ul><ul><li>Careful, there may be a typo or two. His blog site has tips: </li></ul>
  22. 22. Coenraets fds tutorial
  23. 23. Hw – complete the Coenraets tutorial <ul><li>Coenreats uses a hibernate database that comes with the fds-tomcat. Be careful to make copies of various xml config files which you change. </li></ul><ul><li>Good idea to save the original zip to restore settings if things get goofed up. </li></ul><ul><li>In particular, we will later use mysql to interface with flex. This involves changing a properties file which will break the Coenreats’ tutorial webapp. </li></ul>
  24. 24. Other details: classpath <ul><li>To do your own DAO coding, you’ll need more jar files in your class path: spring.jar and flex-messaging.jar </li></ul><ul><li>You can set the classpath as you compile or with a separate instruction: </li></ul><ul><li>C:> sdkTool -classpath classpath1 ; classpath2 ... </li></ul><ul><li>-or- </li></ul><ul><li>C:> set CLASSPATH= classpath1 ; classpath2 ... </li></ul><ul><li>It may be easiest to make a batch file or set the class path environment variable to include the path settings… go to control panel->system->advanced->environment variables->edit classpath </li></ul><ul><li>You can find more details by searching online. </li></ul>
  25. 25. About Java DAO and Flex Accessing Spring beans from flex (FDS) <ul><li>Lin lin has a pretty good tutorial also for building the entire DAO layer by hand and provides a flex client interface also. </li></ul><ul><li>Tutorial URL at </li></ul><ul><li>There may be an error in here, too. Certainly, as with Coenraets, there are a few omissions. </li></ul>
  26. 26. Screenshot: a DAO layer interfacing with MySQL and displayed in Flex, running in the fds-tomcat server
  27. 27. Spring Configuration <ul><li>1). Download spring.jar and flex-spring- factory.jar to your machine, and put them into your flex app’s WEB-INF/lib directory. 2). Add the following into your flex app’s WEB-INF/web.xml file, inside <web-app> tag. </li></ul><ul><li><context-param>    <param-name>contextConfigLocation</param-name>     <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param>  <listener>     <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> </li></ul><ul><li>3). Add the following into to WEB-INFflexservices-config.xml inside <services-config> tag:           <factories>                <factory id=&quot;spring&quot; class=&quot;flex.samples.factories.SpringFactory&quot;/>           </factories> 4). If you are using JRun as app server, set the following in WEB-INFjrun-web.xml to avoid parser conflicting:           <jrun-web-app>               <load-system-classes-first> true</load-system-classes-first>           </jrun-web-app>  Now you are ready to use flex to access Spring beans.  </li></ul>
  28. 28. Properties file for mysql connection <ul><li>In webapps/root/web-inf/src edit the file Be sure to save the old version which shows how to connect to hibernate!!! </li></ul><ul><li>jdbc.driver=com.mysql.jdbc.Driver </li></ul><ul><li>jdbc.url=jdbc:mysql:test ///your_db_name </li></ul><ul><li>jdbc.user=root </li></ul><ul><li>jdbc.password= </li></ul>
  29. 29. Other errata <ul><li>For some reason, I needed to use MyAssembler as well as MyAssemblerSpring… I couldn’t find where the system was looking for it, but I copied both identical class files to my java class directory. Because this file accessed “MyService” I wound up sticking both MyService and MyServiceSpring (identical class files) into my java classes directory. </li></ul><ul><li>Besides the properties file, you need to put database connection details in webapps ootWEB-INFapplicationContext.xml </li></ul><ul><li>see next slide </li></ul>
  30. 30. A piece of Webapps ootWeb-infapplicationContext.xml <ul><li><bean id=&quot;dataSource&quot; class=&quot;org.springframework.jdbc.datasource.DriverManagerDataSource&quot;> </li></ul><ul><li> <property name=&quot;driverClassName&quot; value=&quot;;/> </li></ul><ul><li> <property name=&quot;url&quot; value=&quot;http://localhost:3306/test&quot;/> </li></ul><ul><li> <property name=&quot;username&quot; value=&quot;root&quot;/> </li></ul><ul><li> <property name=&quot;password&quot; value=&quot;&quot;/> </li></ul><ul><li></bean> </li></ul>
  31. 31. Hw: complete lin lin tutorial <ul><li>Complete tutorial. </li></ul><ul><li>Then add reasonable CRUD functionality to your web app. </li></ul>
  32. 32. Automatically generate flex and java code <ul><li> </li></ul><ul><li> </li></ul>
  33. 33. A flex-jrun app example <ul><li> </li></ul><ul><li> (a large pdf on installing and using Flex Builder) </li></ul>