跳轉到內容

XForms/Web 服務

來自華夏公益教科書

可以使用模型中的submission 元素直接從 XForms 應用程式呼叫 Web 服務。

將 Web 服務連線到事件

[編輯 | 編輯原始碼]

要呼叫 Web 服務,首先需要將其“連線”到事件。例如,這可以是表單末尾的“提交”按鈕。

提交元素通常位於簡報中(在 body 標籤內),並連線到模型中的 <submission> 元素。

 <head>
   <xf:model id="my-model">
      <xf:submission id="<b>callWebService</b>">
            <...>
      </xf:submission>
   </xf:model>
 </head>
 <body>
   <xf:submit submission="<b>callWebService</b>">
      <xf:label>Press Button to Call a Web Service</xf:label>
   </xf:submit>
 </body>
</<syntaxhighlight>

== Sample Program ==
The program consists of several parts.

In the model:
# The SOAP submission instance
# The submission
# The SOAP response placeholder

=== SOAP input message ===
Here is an example of the input SOAP message
<pre>
 <!-- What we send to the web service -->
 <xf:instance id="submit-instance" xmlns="http://schemas.xmlsoap.org/soap/envelope/">
   <soap-env:Envelope>
      <soap-env:Body>
          <m:test xmlns:m="http://www.example.com/web-services/my-operation"
            SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
              <string xsi:type="xsd:string">Hello world!</string>
          </m:test>
       </soap-env:Body>
    </soap-env:Envelope>
 </xf:instance>
 <!-- /env:Envelope/env:Body/m:ecrvTestResponse/result -->
</pre>

=== Putting the SOAP Results in an Instance ===
When the web service returns you need to put the returning SOAP response in an instance.  We create another instance in the model and we also need to give it an identifier.  In this case I called it "results-instance".

<syntaxhighlight lang="xml">
 <xf:instance id="results-instance" xmlns="">
    <env:Envelope/> 
 </xf:instance>
 <xf:bind id="results-bind" nodeset="instance('results-instance')/env:Envelope"/>

這只是一個臨時儲存區,結果將被插入到其中。

XForms submission 語句

[編輯 | 編輯原始碼]
 <xf:submission id="send-to-dor-document-web-service"
    action="<nowiki>http://www.example.com/web-service/my-web-service</nowiki>" 
    method="post" 
    mediatype="text/xml"
    ref="instance('submit-instance')"
    replace="instance" 
    instance="results-instance">
    <xf:toggle case="case-busy" ev:event="xforms-submit" />
    <xf:toggle case="case-submit-error" ev:event="xforms-submit-error" />
    <xf:toggle case="case-done" ev:event="xforms-submit-done" />         
 </xf:submission>

需要注意幾件事。action 屬性是 Web 服務連線點的 URL。您呼叫的操作不包含在 URL 中。

在這種情況下,方法(get、put 或 post)是 post。

媒體型別

[編輯 | 編輯原始碼]

XForms 是一種功能豐富的應用程式開發標準。因此,XForms 使用的 HTTP 事務預設情況下使用一種媒體型別,用於包含二進位制檔案和二進位制附件的 XML 應用程式。因此,預設情況下,XForms 會發送包含以下內容的 HTTP 命令

 mediatype="application/xml"

但是,大多數簡單的 Web 服務不支援二進位制。ASCII 文字(不含二進位制)的mediatype 只是

 mediatype="text/xml"

因此,如果您還要傳送二進位制檔案,則需要向提交新增媒體型別屬性

 <xf:submission mediatype="application/xml"...

除錯響應文件

[編輯 | 編輯原始碼]

您還可以用 XML 結果替換整個文件(選項是 replace="all"),或者也可以讓它僅替換例項。

最後三個 <toggle> 語句用於顯示結果。

樣式表

[編輯 | 編輯原始碼]

這使得錯誤訊息為紅色,而正確的響應為綠色。

 <style type="text/css">
     @namespace xf url("http://www.w3.org/2002/xforms");
     body {font-family: Helvetica, sans-serif}
     .code {font-family: Courier New, fixed-width; font-weight:bold;}
     .error-message {font-weight:bold; color: red}
     .ok {font-weight:bold; color: green}
     xf|output {font-weight:bold; font-size: 16pt}
  </style>

這是 XForms 文件的實際主體。它有一個呼叫 Web 服務的單個按鈕。

 <body>
    <h1>Web Service Demo</h1>
         <p>Note: With some browsers (FireFox) you must add the appropriate
         hosts to your XForms "white list" for this application to run.
         With FireFox see Tools/Options/Content tab.</p>
     <xf:submit submission="call-web-service">
         <xf:label>Call Web Service</xf:label>
     </xf:submit>  
     <hr/>
     <p>Current form status:</p>
     <xf:switch>
        <xf:case id="case-start">Status: Ready to call web service.</xf:case>
        <xf:case id="case-busy">Waiting for response...please stand by...</xf:case>
        <xf:case id="case-submit-error">
           <p class="error-message">Submission error. Did you remember to allow XForms to submit data to other domains?</p>
        </xf:case>
        <xf:case id="case-done">
           <p class="ok">Results returned successfully</p>
           <xf:group model="my-model">
             <xf:output ref="instance('results-instance')/env:Body/m:my-results/result">
               <xf:label>Return value: </xf:label>
             </xf:output>
           </xf:group>
         </xf:case>
      </xf:switch>
   </body>

簡報使用 switch/case 部分有條件地顯示錯誤訊息。每個切換元素都由相應的事件觸發。

您還可以在該部分中放置一個旋轉的 GIF 圖示,以便從伺服器獲取響應時獲得動畫。


下一頁: 股票報價 | 上一頁: 搜尋 Flickr
首頁: XForms
華夏公益教科書