Flex 最重要的部分之一就是和服务器以及数据库的通讯。Flex 提供了三个类来与服务器通讯:HTTPService,RemoteObject 以及WebService。
HTTPService 类提供了使用超文本传输协议(HTTP)与服务器通讯的方式。一个Flex 应用程序可以使用GET 或者POST 请求来发送数据到一个服务器并且处理这个请求返回的 XML 或者字符串。使用HTTPService 类,你可以与PHP 页面,ColdFusion 页面,JavaServe页面( jsp),Java servlet, Ruby onRails, 以及ASP 动态网页通讯。
由于本人是Java出身,所以这里就来讨论一下与Servlet的通讯方式。
这里选用MySql数据库,首先建立如下的数据库表
数据库连接
[java] view plain copy public class MyConnection { public Connection conn = null; public MyConnection() { try { // 注册数据库驱动程序为MYSQL驱动 Class.forName("com.mysql.jdbc.Driver"); } catch (java.lang.ClassNotFoundException e) { System.err.println("mydb(): " + e.getMessage()); } try { conn = DriverManager.getConnection( "jdbc:mysql://127.0.0.1:3306/flex", "root", "root"); } catch (SQLException ex) { System.err.println("conn:" + ex.getMessage()); } } public Connection getDbConnection() { return conn; } }
DAO
[c-sharp] view plain copy public class ProductDao { Connection conn; ResultSet rs; Statement stmt; public ProductDao() { conn = new MyConnection().getDbConnection(); try { stmt = conn.createStatement(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public List getProduct() { List list = new ArrayList(); try { String sql = "select * from product"; rs = stmt.executeQuery(sql); while (rs.next()) { String name=rs.getString("name"); String type=rs.getString("type"); double price=Double.parseDouble(rs.getString("price")); int num=Integer.parseInt(rs.getString("num")); Product p=new Product(name,type,price,num); list.add(p); } rs.close(); stmt.close(); conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { return list; } } }
PODO
[java] view plain copy public class Product { private String name; private String type; private double price; private int num; public Product(String name, String type, double price, int num) { this.name = name; this.type = type; this.price = price; this.num = num; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getType() { return type; } public void setType(String type) { this.type = type; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } }
这一部分略过,如果觉得手动部署比较麻烦,我们可以使用MyEclipse插件。值得注意的是web.xml文件的配置,一定要正确配置servlet,下面我给出正确示例
[xhtml] view plain copy <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Benson</display-name> <servlet> <description>This is the description </description> <display-name>This is the display name </display-name> <servlet-name>GetProductServlet</servlet-name> <servlet-class>servlet.GetProductServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>GetProductServlet</servlet-name> <url-pattern>/servlet/GetProductServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <login-config> <auth-method>BASIC</auth-method> </login-config> </web-app>
[xhtml] view plain copy <?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="send()"> <mx:HTTPService id="service" url="http://localhost:8080/servlet/servlet/GetProductServlet" method="GET" /> <mx:Script> <!--[CDATA[ private function send():void{ //发送请求 service.send(); } ]]--> </mx:Script> <mx:Canvas x="0" y="0" width="100%" height="100%"> <mx:DataGrid x="0" y="68" width="676" height="383" id="productdata" dataProvider="{service.lastResult.products.product}" fontFamily="Times New Roman" fontSize="16"> <mx:columns> <mx:DataGridColumn headerText="商品名称" dataField="name"/> <mx:DataGridColumn headerText="商品类别" dataField="type"/> <mx:DataGridColumn headerText="商品价格" dataField="price"/> <mx:DataGridColumn headerText="剩余数量" dataField="num"/> </mx:columns> </mx:DataGrid> <mx:ApplicationControlBar x="0" y="0" height="70" width="676"> </mx:ApplicationControlBar> </mx:Canvas> </mx:Application>
代码重点解释
creationComplete="send()"表明当这个Flex应用创建完成时,就发送这个HttpServer请求给Servlet
url=http://localhost:8080/servlet/servlet/GetProductServlet第一个servlet是我们服务器端的工程名
第二个servelt是servlet所在包名
GetProductServlet使我们真正的servlet名称
dataProvider="{service.lastResult.products.product}" lastResult:在做了一个呼叫到一个HTTPService 之后,数据会从服务返回,并被放置到服务组件所包含的lastResult 对象。由于我们返回的是一个XML数据,所以service.lastResult.products.product就代表返回XML中products标签下的所有product标签内容。Product子标签和Flex中<mx:columns>的dataField属性相对应。
完整的JAVA和FLEX代码可以在这里下载
转载于:https://www.cnblogs.com/arcserver/archive/2012/11/19/2776739.html
相关资源:各显卡算力对照表!