Binding SOAP service to SAPUI5 simpleform

2019-05-26 09:19发布

问题:

I have created a simple form in SAPUI5 with textviews. I have to bind the data from SOAP service to my form. How to do that? Here is the form , service.view.xml

<mvc:View controllerName="themes.controller.service" xmlns:f="sap.ui.layout.form" xmlns:html="http://www.w3.org/1999/xhtml"
xmlns:mvc="sap.ui.core.mvc" xmlns:semantic="sap.m.semantic" xmlns:table="sap.m.table" xmlns="sap.m">
<Bar id="bar4">
    <contentLeft>
        <Button icon="sap-icon://nav-back" press="onNavBack"/>
        <Button icon="sap-icon://home" press="onNavBack"/>
    </contentLeft>
    <contentRight>
        <Button icon="sap-icon://account" press="handleAccount"/>
    </contentRight>
</Bar>
<Page id="pag1">
    <content>

    <f:SimpleForm id="form3" columnsL="1" columnsM="1" editable="false" emptySpanL="4" 
        emptySpanM="4" labelSpanL="3" labelSpanM="3" layout="ResponsiveGridLayout" maxContainerCols="2" minWidth="1024" >
            <f:content>

                <Label text="Country"/>
                <Text text="{country}"/>
                <Label text="Phone Number"/>
                <Text text="{phone}"/>
                <Label text="Carrier"/>
                <Text text="{carrier}"/>
                <Label text="Incoterms"/>
                <Text text="{incoterms}"/>

            </f:content>
        </f:SimpleForm>
    </content>

</Page>
<Bar id="bar5">
    <contentLeft>
        <Button icon="sap-icon://email" press="handleContact"/>
    </contentLeft>
</Bar>

How do I work on the model creation and taking values from the SOAP service? As I am only familiar with OData service please help me in binding the values from SOAP service

回答1:

It can be done fairly easy (and contrary to what Marc commented, you'll need just one $.ajax call) and an sap.ui.model.xml.XMLModel:

// first create your SOAP request envelope
var soapRequest = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + 
                  "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope/\" soap:encodingStyle=\"http://www.w3.org/2003/05/soap-encoding\">" + 
                  "    <soap:Body>" + 
                  "        <m:GetPrice xmlns:m=\"http://www.w3schools.com/prices\">" + 
                  "            <m:Item>Apples</m:Item>" + 
                  "        </m:GetPrice>" + 
                  "    </soap:Body>" + 
                  "</soap:Envelope>";

// instantiate an XMLModel
var oModel = new sap.ui.model.xml.XMLModel();

$.ajax(sURL, { // sURL is the url you are sending the SOAP request to
    method: "POST",
    data: soapRequest,
    dataType: "xml",
    contentType: "text/xml; charset=\"utf-8\""
}).done(function(data, textStatus, jqXHR) {
    // in the 'done' Promise, store the SOAP response into your XMLModel
    oModel.setData(data);
}).fail(function(XMLHttpRequest, textStatus) {
    jQuery.sap.log.fatal("The following problem occurred: " + textStatus, XMLHttpRequest.responseText + "," + XMLHttpRequest.status + "," + XMLHttpRequest.statusText);
});

See https://sapui5.hana.ondemand.com/#docs/api/symbols/sap.ui.model.xml.XMLModel.html for more info