問題:我們希望接收jsp發回來的xml資訊,並存放在datagrid組件裡面去
步驟:
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %><br /> <jsp:useBean id="bean" class="test.Dbconn" scope="page"/><br /><%<br />response.setContentType("text/xml");</p><p>Connection con = null;<br />ResultSet rs=null;<br />con= bean.getConnection();<br />String sql="select * from admin";<br />rs=bean.executeQuery(sql);<br />out.println("<?xml version=/"1.0/" encoding=/"utf-8/"?>");<br />out.println("<userinfo>");<br />while(rs.next())<br />{<br /> out.println("<username>");<br /> out.println("<name>"+rs.getString(2)+"</name>");<br /> out.println("<pwd>"+rs.getString(3)+"</pwd>");<br /> out.println("<sex>"+rs.getString(4)+"</sex>");<br /> out.println("<age>"+rs.getString(4)+"</age>");<br /> out.println("<mail>"+rs.getString(5)+"</mail>");<br /> out.println("</username>");<br />}<br /> out.println("</userinfo>");</p><p>%><br />
我們使用jsp的javabean 來處理mysql資料庫裡面查詢的資訊 並返回給用戶端
用戶端裡面的代碼:
<?xml version="1.0" encoding="utf-8"?><br /><mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="592" height="444" creationComplete="init()"><br /><mx:Script><br /><!--[CDATA[<br />import mx.rpc.events.FaultEvent;<br />import mx.rpc.events.ResultEvent;<br />import Config.Connect2;<br />private var conn:Connect2;<br />[Bindable]<br />private var xml:XML;<br />private function init():void<br />{<br /> conn=Connect2.getconnect();<br />}</p><p>private function senddata():void<br />{<br /> trace("dd");<br /> var para:URLVariables=new URLVariables();<br /> conn.sendMessage(para,Connect2.manurl);<br /> conn.addEventListener(ResultEvent.RESULT,getData);<br /> conn.addEventListener(FaultEvent.FAULT,failHander);<br />}<br />private function getData(event:ResultEvent):void<br />{<br /> xml=XML(event.result);<br /> trace(xml);<br />}<br />private function failHander(event:FaultEvent):void<br />{<br /> trace("cuowu");</p><p>}</p><p>]]--><br /></mx:Script><br /><mx:Button id="Btn_get" click="senddata()" x="439" y="268" label="Get" width="65"/><br /><mx:DataGrid x="80" y="58" width="381" dataProvider="{xml.username}"><br /><mx:columns><br /><mx:DataGridColumn headerText="姓名" dataField="name"/><br /><mx:DataGridColumn headerText="密碼" dataField="pwd"/><br /><mx:DataGridColumn headerText="性別" dataField="sex"/><br /><mx:DataGridColumn headerText="年齡" dataField="age"/><br /><mx:DataGridColumn headerText="郵件" dataField="mail"/><br /></mx:columns><br /></mx:DataGrid></p><p></mx:Canvas><br />
串連的類中增加連結。用於處理用戶端發送的資訊
package Config<br />{<br /> import flash.events.*;<br /> import flash.net.*;</p><p> import mx.rpc.http.HTTPService;<br /> public class Connect2 extends HTTPService<br /> {<br /> private static var connect:Connect2=null; </p><p> //串連的網址<br /> public static var myurl:String="http://localhost:8080/flash/receive.jsp";<br /> public static var regurl:String="http://localhost:8080/flash/Reg.jsp";<br /> public static var figheurl:String="http://localhost:8080/flash/Fight.jsp";<br /> public static var manurl:String="http://localhost:8080/flash/Manager.jsp";<br /> public function Connect2()<br /> {<br /> this.method="POST";<br /> this.useProxy=false; //不啟動代理服務<br /> this.resultFormat="xml";//設定返回資料的格式為xml<br /> } </p><p> public static function getconnect():Connect2<br /> {<br /> if (connect == null)<br /> {<br /> connect=new Connect2();<br /> }<br /> return connect;<br /> }<br /> public function sendMessage(para:URLVariables,myurl:String):void<br /> { //設定發送資料的變數<br /> this.url=myurl;//指定發送的jsp頁面<br /> this.send(para);//發送資料<br /> }<br /> }<br />}<br />
<?xml version="1.0" encoding="utf-8"?><br /><mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()" width="703" height="543"><br /><mx:Script><br /><!--[CDATA[<br />import View.ManagerPannel;</p><p> private function init():void<br /> {<br /> var wnd:ManagerPannel=new ManagerPannel();<br /> addChild(wnd);<br /> }<br />]]--><br /></mx:Script></p><p></mx:Application><br />
原理:jsp輸出xml格式,然後返回給用戶端,用戶端使用datagrid組件來存放jsp 返回的資訊。一一對位 裡面是返回的xml結果。
userinfo><br /> <username><br /> <name>d</name><br /> <pwd>d</pwd><br /> <sex>3</sex><br /> <age>3</age><br /> <mail>2</mail><br /> </username><br /> <username><br /> <name>d</name><br /> <pwd>2</pwd><br /> <sex>2</sex><br /> <age>2</age><br /> <mail>2</mail><br /> </username><br /> <username><br /> <name>jim</name><br /> <pwd>123</pwd><br /> <sex>b</sex><br /> <age>b</age><br /> <mail>23</mail><br /> </username><br /> <username><br /> <name>d</name><br /> <pwd>d</pwd><br /> <sex>d</sex><br /> <age>d</age><br /> <mail>2</mail><br /> </username><br /></userinfo></p><p>