I have been learning flex for more than ten days since the call center project began! Now, I have basically entered the status. Today, I am here to make an example of using Flex to communicate with the backend servlet, hoping to help some new users who just learned flex. To figure out how Flex communicates with the background, I did nothing five days before I started to learn Flex. Today, I am posting this post to help the following brothers avoid detours.
Interaction principle: the three letters --------- XML, the client receives XML data sent from the server.
In this example, the client is a DataGrid component to display the data transmitted by the server. The corresponding mxml file is as follows:
XML Code
- <? XML version = "1.0" encoding = "UTF-8"?>
- <Mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml" layout = "absolute">
- <Mx: model source = "http: // localhost: 8080/flex/First" id = "model">
- </MX: Model>
- <Mx: panel title = "User Information" width = "776" Height = "281" fontsize = "18">
- <Mx: DataGrid dataprovider = "{model. User}" width = "748" Height = "231">
- <Mx: columns>
- <Mx: datagridcolumn datafield = "name" headertext = "user">
- </MX: datagridcolumn>
- <Mx: datagridcolumn datafield = "PWD" headertext = "password">
- </MX: datagridcolumn>
- <Mx: datagridcolumn datafield = "school" headertext = "">
- </MX: datagridcolumn>
- </MX: columns>
- </MX: DataGrid>
- </MX: Panel>
- </MX: Application>
Note the <mx: Model> label. The source attribute specifies a servlet ing. The servlet uses response to write XML to the client.
Read database data and generate an XML file consisting of two classes: firstservlet. java, one is XML. java, the former is a common Servlet used to write XML files, and the latter is used to generate xml files, completed by Java xml api operations. Take a look at the code first.
Firstservlet code Abstract:
Java code
- Try {
- XML. INIT ();
- Class. forname ("com. Microsoft. JDBC. sqlserver. sqlserverdriver ");
- // ********** Create a database named flex *************//
- String url = "JDBC: Microsoft: sqlserver: // localhost: 1433; databasename = Flex ";
- // *********** Change your username and password information ************//
- Con = drivermanager. getconnection (URL, "sa", "135780 ");
- Stmt = con. createstatement ();
- // Create a table named users in ********** for details about the SQL statement, see Attachment download **********//
- Result = stmt.exe cutequery ("select * from users ");
- } Catch (exception e ){
- E. printstacktrace ();
- }
- // Important: Set the response format to XML.
- Response. setcontenttype ("text/XML ");
- Response. setcharacterencoding ("UTF-8 ");
- Printwriter out = response. getwriter ();
The above code connects the servlet to the database and sets the response format. below is the key code for writing the XML file:
Java code
- While (result. Next ()){
- String [] STRs = new string [3];
- STRs [0] = result. getstring ("name ");
- STRs [1] = result. getstring ("PWD ");
- STRs [2] = result. getstring ("school ");
- // ***** Create xmldocument *******//
- XML. Create (STRs );
- }
- Result. Close ();
- Stmt. Close ();
- Con. Close ();
- // ******* Write the XML file to the client ********//
- XML. Output (out );
The XML of the last line is an example of XML. java. Some important methods of XML are as follows:
Java code
- /**
- * Write the XML document to the output stream
- *
- * @ Param out
- * ---- Specified output stream
- * @ Throws exception
- *
- * Prepared by: Wang jinghui Hunan Agricultural University & Hunan airuijie Investment Management Company
- */
- Public void output (writer) throws exception {
- Transformer trans = transformerfactory. newinstance (). newtransformer ();
- Trans. setoutputproperty (outputkeys. encoding, "UTF-8 ");
- Source source = new domsource (document );
- Result result = new streamresult (writer );
- Trans. Transform (source, result );
- Writer. Flush ();
- Writer. Close ();
- }
Java code
- /**
- * Create an XML document
- *
- * @ Param STRs
- * ------- Transmitted name and password and school Parameters
- *
- * Prepared by: Wang jinghui Hunan Agricultural University & Hunan airuijie Investment Management Company
- */
- Public void create (string [] STRs ){
- // ****** Level 1 subnode ******//
- Element first = Document. createelement ("user ");
- Root. appendchild (first );
- For (INT I = 0; I <STRs. length; I ++ ){
- If (I = 0 ){
- // ******* Level 2 subnode ******//
- Element name = Document. createelement ("name ");
- Name. appendchild (document. createtextnode (STRs [I]);
- First. appendchild (name );
- } If (I = 1 ){
- Element Pwd = Document. createelement ("PWD ");
- PWD. appendchild (document. createtextnode (STRs [I]);
- First. appendchild (PWD );
- } If (I = 2 ){
- Element school = Document. createelement ("school ");
- School. appendchild (document. createtextnode (STRs [I]);
- First. appendchild (school );
- }
- }
- }