日期:2014-05-17 浏览次数:21127 次
var xmlhttp;
//ajax方法
function createXMLHttpRequest(){
if(window.ActionXObject){
xmlhttp=new ActionXObject("Microsoft.XMLHTTP");
}else if(window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}
alert("ajax启动");
}
//showList()
function showList(){
createXMLHttpRequest();
xmlhttp.open("GET","getList.action?date="+new Date().getTime(),true);
xmlhttp.onreadystatechange = returnListString;
xmlhttp.send();
alert("showList启动");
}
//从action取回数据
function returnListString(){
if(xmlhttp.readyState==4){
alert("xmlhttp.readyState==4");
alert("xmlhttp.status"+xmlhttp.status);
if(xmlhttp.status==200){
var returnJsonString = xmlhttp.responseText;
var jsonString=JSON.parse(returnJsonString);
var showString="";
for(var i=0;i<jsonString.length;i++){
showString+=jsonString[i]+"\n";
}
alert(showString);
$("#show").html(showString);
}
}
}
public class UrlString extends ActionSupport {
	
	public void  listString() throws IOException{
		List list = new ArrayList();
		list.add("zjl");
		list.add("123");
		JSONArray jsonArray = JSONArray.fromObject(list);
		System.out.println("jsonArray="+jsonArray.toString());
		
		ServletActionContext.getResponse().setContentType("text/html");
		ServletActionContext.getResponse().setCharacterEncoding("utf-8");
		ServletActionContext.getResponse().getWriter().printf(jsonArray.toString());
		ServletActionContext.getResponse().getWriter().flush();
		ServletActionContext.getResponse().getWriter().close();
	} 
}
<package name="actionToAjax" extends="json-default">
<action name="getList" class="com.controller.UrlString" method="listString">
</action>
</package>