对于建立一个flex web应用的步骤我就不多说了哈。
首先要求大家到数据库建立一个数据库,在我这例子中数据库名为userDb,建立一个表userInfo,里面设置两个字段一个为id,一个为name.第二,创建以下java代码在src目录下
User.java
package org.rjb.java; public class User { private Stringname; public StringgetName() { return name; } public void setName(Stringname) { this .name = name; } } UserDao.java package org.rjb.java; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; public class UserDao { public static ConnectiongetConnection() { Connectionc = null ; try { Stringdriver = " com.mysql.jdbc.Driver " ; Stringurl = " jdbc:mysql://localhost:3306/userDb " ; Stringusername = " root " ; Stringpassword = " 1235 " ; Class.forName(driver); c = DriverManager.getConnection(url,username,password); } catch (Exceptione) { System.out.println(e.getMessage()); } return c; } public ListgetAllUser() throws SQLException { Connectionc = getConnection(); Statementst = c.createStatement(); ResultSetrs = st.executeQuery( " select*fromUserInfo " ); ArrayListuserList = new ArrayList < User > (); while (rs.next()) { Useru = new User(); u.setName(rs.getString( " name " )); userList.add(u); } return userList; } } |
以上是java端的全部代码了,下面是flex端的代码。
第三,创建以下代码在flex_src下:
User.as
package org.rjb.flex { [RemoteClass(alias = " org.rjb.java.User " )] public class User { private var_name:String; public functionUser() { } public functiongetname():String { return this ._name; } public functionsetname(name:String): void { this ._name = name; } } } User.as package org.rjb.flex { [RemoteClass(alias = " org.rjb.java.User " )] public class User { private var_name:String; public functionUser() { } public functiongetname():String { return this ._name; } public functionsetname(name:String): void { this ._name = name; } } } |
接下来是mxml代码,也就是我们真正展示我们数据的界面
FlexWith
Java
.mxml
<? xmlversion="1.0"encoding="utf-8" ?> < mx:Application xmlns:mx ="http://www.adobe.com/2006/mxml" layout ="vertical" > < mx:RemoteObject id ="ud" destination ="UserDao" result ="onResult(event)" fault ="onFault()" /> < mx:DataGrid dataProvider ="{users}" > < mx:columns > < mx:DataGridColumn dataField ="name" headerText ="name" /> </ mx:columns > </ mx:DataGrid > < mx:Label id ="info" /> < mx:Button label ="click" click ="clickFun()" /> < mx:Script > <![CDATA[ importmx.collections.ArrayCollection; importmx.rpc.events.ResultEvent; [Bindable] publicvarusers:ArrayCollection=newArrayCollection(); publicfunctionclickFun():void{ ud.getAllUser(); } publicfunctiononResult(event:ResultEvent):void{ users=ArrayCollection(event.result); info.text="getdatasuccessfully"; } publicfunctiononFault():void{ info.text="Error"; } ]]> </ mx:Script > </ mx:Application > |
第四,配置remoting-config.xml,在里面加上如下配置文件:
<
destination
id
="UserDao"
>
<
properties
>
<
source
>
org.rjb.java.UserDao
</
source
>
</
properties
>
</
destination
>