1. schema約束javascript
一 幾個重要知識: html
1 . namespace 至關於Schema文檔的id,它的值必須是惟一java
2. targetNamespace 屬性用來指定schema文檔的namespace值jquery
3. xmlns 屬性 引入某個命名空間web
<?xml version="1.0" encoding="UTF-8"?> <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.zhouwuji.cn" elementFormDefault="qualified"> <!-- qualified 關聯約定全部的標籤 默認爲unqualified --> <element name="books"> <complexType> <!-- 複合類型 unbounded 無限的 --> <sequence maxOccurs="unbounded"> <element name="book"> <complexType> <sequence maxOccurs="1"> <element name="bookname" type="string" /> <element name="author" type="string" /> <element name="price" type="string" /> </sequence> </complexType> </element> </sequence> </complexType> </element> </schema>
<?xml version="1.0" encoding="UTF-8"?> <books xmlns="http://www.zhouwuji.cn" xmlns:ss="http://www.w3.org/2001/XMLSchema-instance" ss:schemaLocation="http://www.zhouwuji.cn test.xsd"> <book> <bookname>javascript</bookname> <author>淘氣老師</author> <price>¥32.1</price> </book> </books> <!-- schema規範中: 1.全部的標籤和屬性都須要有schema文件來定義 2.全部的schema文件都須要有一個id,但在這裏他叫namespace 3.namespace的值由什麼來決定? 有targetNamespace屬性來指定,必須制定schema文件的位置 4.如何引用一個schema約束? 屬性:用xmlns屬性 屬性值:對應的schema文件的id 5.若是引入的schema不是w3c組織定義,必須指定schema文件的位置 6.shcema文件的位置有什麼屬性指定? 屬性:schemaLocation 屬性值:namespace path 7.若是引入N個約束,須要給n-1個取別名 -->
2 關於 Web Service 的幾個問題ajax
四、 爲何要用 Web service? spring
web service 能解決: 跨平臺調用 、跨語言調用 、遠程調用apache
3. Web Service 中的幾個重要術語跨域
8 CXF 框架的深刻使用 查看代碼瀏覽器
function reqWebService() { var name = document.getElementById("name").value; var date = '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Header><authHeader><userId>wl</userId><password>1985310</password></authHeader>
</soap:Header><soap:Body><ns2:getOrderById xmlns:ns2="http://ws.zhouwuji.com/"><arg0>' + name + '</arg0></ns2:getOrderById></soap:Body></soap:Envelope>'; var xmlhttp = getRequest(); xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { var result = xmlhttp.responseXML; var returnEle1 = result.getElementsByTagName("id")[0]; var returnEle2 = result.getElementsByTagName("name")[0]; var returnEle3 = result.getElementsByTagName("price")[0]; var vale1 = returnEle1.firstChild.data; var vale2 = returnEle2.firstChild.data; var vale3 = returnEle3.firstChild.data; alert(vale1+vale2+vale3); } }; //localhost 不能用ip請求 對於js講跨域請求 協議/ip/端口 保持一致 不然跨域 xmlhttp.open("POST","http://localhost:8080/webservicesCXF-spring/orderws"); xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); xmlhttp.send(date); } function getRequest() { var xmlhttp = null; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp = new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } return xmlhttp; }
$("#btn1").click(function() { var name = document.getElementById("name").value; var date = '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Header><authHeader><userId>wl</userId><password>1985310</password>
</authHeader> </soap:Header><soap:Body><ns2:getOrderById xmlns:ns2="http://ws.zhouwuji.com/"><arg0>' + name + '</arg0></ns2:getOrderById></soap:Body></soap:Envelope>'; alert(date); $.ajax({ type : "POST", url : "http://localhost:8080/webservicesCXF-spring/orderws", data : date, success : function(msg) { alert("------"); var $Result = $(msg); var value = $Result.find( "return").text(); alert(value); }, error : function(msg) { //alert("-----"+msg); }, dataType : "xml" }); });
String path = "http://localhost:8080/webservicesCXF-spring/orderws"; String data = "<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'><soap:Header><authHeader><userId>wl</userId><password>1985310</password>
</authHeader></soap:Header><soap:Body><ns2:getOrderById xmlns:ns2='http://ws.zhouwuji.com/'><arg0>" + name + "</arg0></ns2:getOrderById></soap:Body></soap:Envelope>"; URL url = new URL(path); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestProperty("Content-Type", "text/xml;charset=utf-8"); OutputStream os = connection.getOutputStream(); os.write(data.getBytes("utf-8")); os.flush(); int code = connection.getResponseCode(); if(code==200){ InputStream is = connection.getInputStream(); response.setContentType("text/xml;charset=utf-8"); ServletOutputStream outputStream = response.getOutputStream(); int len=0; byte[] buff = new byte[1024]; while((len = is.read(buff))>0){ outputStream.write(buff,0,len); } outputStream.flush(); outputStream.close(); } }
b @WebMethod
c @WebResult
d @WebParam
e @XmlElement
注: 說明 即便是沒有修改源代碼,只修改了註解,客戶端的代碼也必需要重 新生成, 不然調用將會失敗。