日期:2014-05-16  浏览次数:20415 次

使用prototype.js经验积累
1、Ajax返回xml格式的对象
1)前台js
function viewIndexGrade(id){
	var offsetObj = event.srcElement;
	var url = "<ww:url value='/xxxAction!getIndexGrades.action'/>?obj.id=" + id;
	var http = new Ajax.Request(url,
		{method: 'get',
		asynchronous: false,
		onComplete:function(oriReq){
			var divObj = document.getElementById("indexGradDiv");
			var sb = "<table class='table-border' align='center' cellspacing='1' cellpadding='1' width='100%'>";
			var responseDoc = oriReq.responseXML;
			var records = responseDoc.documentElement.childNodes;
			if(records.length > 0){
				for(var i = 0; i < records.length; i++){
					var indexCode = records[i].selectSingleNode("indexCode").text;
					var grade = records[i].selectSingleNode("grade").text;
					sb += "<tr>";
					sb += "<td class='table-title' style='height:25px' align='left' width='40%'>" + indexCode + "</td>";
					sb += "<td class='table-content' style='height:25px' align='center'>" + grade + "</td>";
					sb += "</tr>";
				}
				sb += "</table>";
				divObj.style.height = (25 * records.length + 10) + "px";
			}else{
				sb += "<tr>";
				sb += "<td class='table-content' align='center'>无调查数据</td>";
				sb += "</tr>";
				sb += "</table>";
				divObj.style.height = "40px";
			}
			divObj.innerHTML = sb;
			while(offsetObj.tagName != "A"){
				offsetObj = offsetObj.parentElement;
			}
			viewDiv(divObj, offsetObj, true, 0, 0);
		}
	});
}
</SCRIPT>


2)webwork配置
<xwork>
    <package name="XXX" extends="default">
		<action name="xxxAction" class="com.***.***.action.***Action">

			<result name="xml" type="stringreader">
				<param name="inputName">stringReader</param>
				<param name="contentType">text/xml;charset=gb2312</param>
				<param name="parse">false</param>
				<param name="bufferSize">1024</param>
			</result>
			<interceptor-ref name="saveValidationStack"/>
		</action>
	</package>
</xwork>

3)后台action类处理
protected String xmlStr = "";
public StringReader getStringReader() {
		return new StringReader("<?xml version=\"1.0\" encoding=\"GBK\"?>"
				+ xmlStr);
	}

public String getIndexGrades() {
		xmlStr = ***Service.getIndexGrades(obj.getId());
		
		return "xml";
	}


getIndexGrades方法返回的xml格式是:
<root>
<record>
<indexCode><![CDATA[INDEX]]></indexCode>
<grade><![CDATA[97.81]]></grade>
</record>
</root>

如果没有拼接xml格式的文件头则js取不到xml对象
<?xml version=\"1.0\" encoding=\"GBK\"?>