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

不借助中间页面实现JSON数据的接收

上代码:

markuplayer1.jsp:

function doTrClick(id,index){				
		var url = "<%=basePath%>markuplayer1.do?method=read";
		var pars = "id=" + id;
		var ajax = new Ajax.Request(
			 url,
			{method:'post',parameters:pars,onComplete:checkreq}
	        ); 
	}

?

function checkreq(request){
  		var info = request.responseText; 
		var info2 = eval('('+info+')'); 
		var re = info2.msg;
 		
		if(re == "success"){
			g_countryArray = info2.countryArr;	//注意地方	sendJSON中填充的数据  
			openChild("<%=basePath%>master/markuplayer/markuplayer1Details.jsp",750,380);
		}
	}

?Java页面处理:

?

String countryCodes = markuplayer1.getCountrycode();
		JSONArray countriesArray = new JSONArray();
		//set countrycodes to json array
		if(countryCodes != null && !"".equals(countryCodes)) {
			String[] countryArray = countryCodes.split(",");
			if(countryArray.length > 0) {
				List<Countries> countries = (List<Countries>)request.getSession().getServletContext().getAttribute(MyConstants.COUNTRIES_KEY);	
				for(String countrycode : countryArray) {
					for(Countries country : countries) {
						if(countrycode.trim().equalsIgnoreCase(country.getCountrycode().trim())) {
							JSONObject json = new JSONObject();
							json.put("code", country.getCountrycode().trim());
							json.put("countrynameEn", country.getCountrynameEn());
							json.put("countrynameCn", country.getCountrynameCn());
							json.put("countrynameTw", country.getCountrynameTw());
							countriesArray.add(json);
						}
					}
				}
			}
		}

JSONUtils.sendJSON(response, "success",countriesArray);

?关键部分:

public static void sendJSON(HttpServletResponse response,String message, JSONArray countrycontent) {
		response.setContentType("application/json");
		response.setCharacterEncoding("utf-8");
		PrintWriter outResponse = null;
		try {
			outResponse = response.getWriter();

			JSONObject jsonObject = new JSONObject();
			jsonObject.put("msg", message);
			jsonObject.put("countryArr", countrycontent);

			outResponse.print(jsonObject.toString());
			outResponse.flush();
		} catch (IOException e) {
			log.error("json utils exception :" + e);
		} finally {
			if(outResponse != null) {
				outResponse.close();
			}
		}
	}

?这样jsp页面就可以接收到JSONArray类型的countryArr,至此结束!

?

扩展:

markuplayer1Detail.jsp使用JSONArray类型的数据:

 var g_countryList =  [] ;
wwindow.onload = changeToReservations;
	 	function changeToReservations(){
			g_countryList = window.parent.g_countryArray;			
		}

?点Edit弹出一个页面并利用接收到的JSONArray类型来填充数据:

<span id="genCountryList" onmouseover='this.style.cursor="pointer";' onClick="genCountryListTable('idCountryList');">
										Edit	
									</span>
?
var g_countryList  = [];
function genCountryListTable( id ) {
	  			  			
			var obj = document.getElementById(id);
			if(obj) {
	  			obj.style.display = '';
	  		}
	  		
			var sCountryTable        = [];
			sCountryTable[sCountryTable.length] = "<table style='width:100%;font-size: 11px;'>";
			
			for (var i=0; i<g_countryList.length; i++)	{		
				sCountryTable[sCountryTable.length] = '<tr id="tr_'+i+'" onmouseover="domouseOver(this);" onmouseout="domouseOut(this);">';			
				sCountryTable[