【转】Working with JavaServer Pa…

来源:互联网 发布:速压是什么软件 编辑:程序博客网 时间:2024/05/22 01:44

Working with JavaServer Pages

You can use a Flex HTTPService component in conjunction with aJSP page and a SQL database management system to display theresults of a database query in a Flex application and insert datainto a database. You can call a JSP page with GET or POST toperform a database query. You can then format the query result datain an XML structure and return the XML structure to the Flexapplication in the HTTP response. When the result has been returnedto the Flex application, you can display it in one or more userinterface controls.

MXML code

The Flex application in the following example calls a JSP pagethat retrieves data from a SQL database. It formats database queryresults as XML and returns the XML to the Flex application, whereit is bound to the dataProvider property of a DataGrid controland displayed in the DataGrid control.

<?xml version="1.0" encoding="utf-8"?><mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">        <mx:HTTPService id="srv" url="catalog.jsp"/>    <mx:DataGrid dataProvider="{srv.lastResult.catalog.product}" width="100%" height="100%"/>         <mx:Button label="Get Data" click="srv.send()"/>    </mx:Application>

The HTTPService's send() method makes the call to the JSPpage. This call is made in the click event of the Button in the MXMLfile.

The resultFormatproperty of the HTTPService component is set to object, so the data is sent back to theFlex application as a graph of ActionScript objects. This is thedefault value for the resultFormat property. Alternatively, youcan use a result format of e4x to return data as an XMLList object onwhich you can perform ECMAScript for XML (E4X) operations.Switching the resultFormatproperty to e4x requiresthe following minor changes to the MXML code.

Note: If the result format is e4x, you do not include the root nodeof the XML structure in the dot notation when binding to theDataGrid.

The XML returned in this example contains no namespaceinformation. For information about working with XML that doescontain namespaces,

...    <mx:HTTPService id="srv" url="catalog.jsp" resultFormat="e4x"/>...    <mx:DataGrid dataProvider="{srv.lastResult.product}" width="100%" height="100%"/>

When using the e4x result format, you can optionally bind thelastResult property to anXMLListCollection object and then bind that object to theDataGrid.dataProvider property:

...    <mx:XMLListCollection id="xc"         source="{userRequest.lastResult.product}"/>              <mx:DataGrid id="dgUserRequest" x="22" y="128" dataProvider="{xc}">...

JSP code

The following example shows the JSP page used in thisapplication. This JSP page does not call a database directly. Itgets its data from a Java class called ProductService, which inturn uses a Java class called Product to represent individualproducts.

<%@page import="flex.samples.product.ProductService,                 flex.samples.product.Product,                 java.util.List"%><?xml version="1.0" encoding="utf-8"?><catalog><%    ProductService srv = new ProductService();    List list = null;    list = srv.getProducts();    Product product;    for (int i=0; i<list.size(); i++)    {        product = (Product) list.get(i);%>    <product productId="<%= product.getProductId()%>"><name><%= product.getName() %></name><description><%= product.getDescription() %></description><price><%= product.getPrice() %></price><image><%= product.getImage() %></image><category><%= product.getCategory() %></category><qtyInStock><%= product.getQtyInStock() %></qtyInStock></product><%    }%></catalog>

Calling HTTP services in ActionScript

The following example shows an HTTP service call in anActionScript script block. Calling the useHTTPService() method declares theservice, sets the destination, sets up result and fault eventlisteners, and calls the service's send() method.

<?xml version="1.0"?><!-- fds\rpc\HttpServiceInAS.mxml. Compiles --> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" verticalGap="10">    <mx:Script>        <![CDATA[            import mx.controls.Alert;            import mx.rpc.http.HTTPService;            import mx.rpc.events.ResultEvent;            import mx.rpc.events.FaultEvent;                        private var service:HTTPService             public function useHttpService(parameters:Object):void {                service = new HTTPService();                service.destination = "sampleDestination";                service.method = "POST";                service.addEventListener("result", httpResult);                service.addEventListener("fault", httpFault);                service.send(parameters);            }            public function httpResult(event:ResultEvent):void {                var result:Object = event.result;            //Do something with the result.            }            public function httpFault(event:FaultEvent):void {                var faultstring:String = event.fault.faultString;                Alert.show(faultstring);            }        ]]>    </mx:Script></mx:Application>