问题:我们希望接收jsp发回来的xml信息,并存放在datagrid组件里面去
步骤:
<textarea cols="50" rows="15" name="code" class="c-sharp"><%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %> <jsp:useBean id="bean" class="test.Dbconn" scope="page"/> <% response.setContentType("text/xml"); Connection con = null; ResultSet rs=null; con= bean.getConnection(); String sql="select * from admin"; rs=bean.executeQuery(sql); out.println("<?xml version=/"1.0/" encoding=/"utf-8/"?>"); out.println("<userinfo>"); while(rs.next()) { out.println("<username>"); out.println("<name>"+rs.getString(2)+"</name>"); out.println("<pwd>"+rs.getString(3)+"</pwd>"); out.println("<sex>"+rs.getString(4)+"</sex>"); out.println("<age>"+rs.getString(4)+"</age>"); out.println("<mail>"+rs.getString(5)+"</mail>"); out.println("</username>"); } out.println("</userinfo>"); %> </textarea>
我们使用jsp的javabean 来处理mysql数据库里面查询的信息 并返回给客户端
客户端里面的代码:
<textarea cols="50" rows="15" name="code" class="c-sharp"><?xml version="1.0" encoding="utf-8"?> <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="592" height="444" creationComplete="init()"> <mx:Script> <!--[CDATA[ import mx.rpc.events.FaultEvent; import mx.rpc.events.ResultEvent; import Config.Connect2; private var conn:Connect2; [Bindable] private var xml:XML; private function init():void { conn=Connect2.getconnect(); } private function senddata():void { trace("dd"); var para:URLVariables=new URLVariables(); conn.sendMessage(para,Connect2.manurl); conn.addEventListener(ResultEvent.RESULT,getData); conn.addEventListener(FaultEvent.FAULT,failHander); } private function getData(event:ResultEvent):void { xml=XML(event.result); trace(xml); } private function failHander(event:FaultEvent):void { trace("cuowu"); } ]]--> </mx:Script> <mx:Button id="Btn_get" click="senddata()" x="439" y="268" label="Get" width="65"/> <mx:DataGrid x="80" y="58" width="381" dataProvider="{xml.username}"> <mx:columns> <mx:DataGridColumn headerText="姓名" dataField="name"/> <mx:DataGridColumn headerText="密码" dataField="pwd"/> <mx:DataGridColumn headerText="性别" dataField="sex"/> <mx:DataGridColumn headerText="年龄" dataField="age"/> <mx:DataGridColumn headerText="邮件" dataField="mail"/> </mx:columns> </mx:DataGrid> </mx:Canvas> </textarea>
连接的类中增加链接。用于处理客户端发送的信息
<textarea cols="50" rows="15" name="code" class="c-sharp">package Config { import flash.events.*; import flash.net.*; import mx.rpc.http.HTTPService; public class Connect2 extends HTTPService { private static var connect:Connect2=null; //连接的网址 public static var myurl:String="http://localhost:8080/flash/receive.jsp"; public static var regurl:String="http://localhost:8080/flash/Reg.jsp"; public static var figheurl:String="http://localhost:8080/flash/Fight.jsp"; public static var manurl:String="http://localhost:8080/flash/Manager.jsp"; public function Connect2() { this.method="POST"; this.useProxy=false; //不启动代理服务 this.resultFormat="xml";//设置返回数据的格式为xml } public static function getconnect():Connect2 { if (connect == null) { connect=new Connect2(); } return connect; } public function sendMessage(para:URLVariables,myurl:String):void { //设置发送数据的变量 this.url=myurl;//指定发送的jsp页面 this.send(para);//发送数据 } } } </textarea>
<textarea cols="50" rows="15" name="code" class="c-sharp"><?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()" width="703" height="543"> <mx:Script> <!--[CDATA[ import View.ManagerPannel; private function init():void { var wnd:ManagerPannel=new ManagerPannel(); addChild(wnd); } ]]--> </mx:Script> </mx:Application> </textarea>
原理:jsp输出xml格式,然后返回给客户端,客户端使用datagrid组件来存放jsp 返回的信息。一一对位 里面是返回的xml结果。
<textarea cols="50" rows="15" name="code" class="c-sharp">userinfo> <username> <name>d</name> <pwd>d</pwd> <sex>3</sex> <age>3</age> <mail>2</mail> </username> <username> <name>d</name> <pwd>2</pwd> <sex>2</sex> <age>2</age> <mail>2</mail> </username> <username> <name>jim</name> <pwd>123</pwd> <sex>b</sex> <age>b</age> <mail>23</mail> </username> <username> <name>d</name> <pwd>d</pwd> <sex>d</sex> <age>d</age> <mail>2</mail> </username> </userinfo> </textarea>