j2eeSSH简单利用JSON实现异步查询

简单的异步,根据多种条件查询,统计出符合条件的结果统计数量,利用JSON传输数据,把体统计的数量(数据库查出)拼装成JSON,传给JSP页面,在根据传过来的JSON数据进行显示。当查询条件变动或者修改,再点击查询按钮不需要整个页面刷新,只需下面显示结果的部分标签区域刷新,另外一大优点是如果不用异步点击查询按钮之后之前填写好的查询条件会清除,异步之后不会有任何改动,比较人性化,用户体验大大提升。

</pre><p>查询条件查询按钮代码:</p><p><input type="submit" value="查找" οnclick="submitForm()" /></p><p><span style="white-space:pre"></span>JS代码:</p><p><span style="white-space:pre"></span></p><pre code_snippet_id="414648" snippet_file_name="blog_20140702_3_8111299" name="code" class="javascript">function submitForm() {
				      startRequest(1);
		} 


//开始异步请求
		function startRequest() {  		  
			//取得页面参数值,作为查询条件传到后台进行查询
			 var startTime = document.getElementById("startdate").value;	//开始时间
			 var endTime = document.getElementById("enddate").value;	//结束时间
			 var state = document.getElementById("state").value;	
			 var eid = document.getElementById("eid").value;	
			 var CPhone = document.getElementById("receivePhone").value;	
			 var dptId = document.getElementById("dptId").value;	
			 var cbNo = document.getElementById("sellerCode").value;	//商家编号
			 var sellerName = document.getElementById("sellerName").value;	//商家名称 
				   	
			 xmlHttp.onreadystatechange = function() {	 
				handleStateChange();
			 };
			 xmlHttp.open("POST","pointInOrderAction!findTotalAgencyOrder",true); //请求地址,true表示开启异步,中间为请求地址,非异步就为form的action的提交地址,下面会给出findTotalAgencyOrder方法
			 xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			 xmlHttp.send("startTime=" + startTime + "&endTime="+endTime + "&state="+state + "&eid="+eid
				  + "&CPhone="+CPhone+ "&dptId="+dptId+ "&cbNo="+cbNo+ "&sellerName="+sellerName);   //发送请求参数,action中setget
		} 
<pre name="code" class="java">public void findTotalAgencyOrder() {
		
		HttpServletResponse response = ServletActionContext.getResponse();
		String sbuf =  pointInOrderService.totalAgencyOrder(str);
   	 	try {
			response.setContentType("text/html;charset=UTF-8");
			response.getWriter().println(sbuf);  //把数据传到jsp页面
		} catch (IOException e) {
			e.printStackTrace();
		}	
	}

 
 
public String totalAgencyOrder(String str) {   //pointInOrderService.totalAgencyOrder中代码		
	int totalAgency = dao.findtotalAgency(); //数据库查找出的数,此行为伪代码
	int totalDaodianAgency = dao.findtotalAgency(); //数据库查找出的数,此行为伪代码
	int totalGuoqiAgency = dao.findtotalAgency(); //数据库查找出的数,此行为伪代码
	int totalQuhuoAgency = dao.findtotalAgency(); //数据库查找出的数,此行为伪代码
	int totalJushouAgency = dao.findtotalAgency(); //数据库查找出的数,此行为伪代码
		//拼装JSON
		StringBuffer strBuffer = new StringBuffer();
		strBuffer.append("{");
		strBuffer.append("\"totalDaodian\":");
		strBuffer.append(totalDaodianAgency);
		strBuffer.append(",");
		strBuffer.append("\"totalGuoqi\":");
		strBuffer.append(totalGuoqiAgency);
		strBuffer.append(",");
		strBuffer.append("\"totalQuhuo\":");
		strBuffer.append(totalQuhuoAgency);
		strBuffer.append(",");
		strBuffer.append("\"totalJushou\":");
		strBuffer.append(totalJushouAgency);
		strBuffer.append(",");
		strBuffer.append("\"totalAll\":");
		strBuffer.append(totalAgency);
		strBuffer.append("}");
		//json结束
		
		return strBuffer.toString();
	}
<pre name="code" class="javascript">//处理显示
				function handleStateChange() {
					if (xmlHttp.readyState == 4) {
						if (xmlHttp.status == 200) {
							var text = (xmlHttp.responseText);  //接收action中的JOSN传过来的数据
							var jsonObj = eval('(' + text + ')');
							var jsonObject = JSON.parse(text);	  //转化JOSN格式,JAVA格式转为JSJson,根据该对象直接取值,根据KEY值,直接JS赋值
					        document.getElementById("th1").innerText = jsonObject.totalDaodian;
					        document.getElementById("th2").innerText = jsonObject.totalGuoqi;
					        document.getElementById("th3").innerText = jsonObject.totalQuhuo;
					        document.getElementById("th4").innerText = jsonObject.totalJushou;
					        document.getElementById("th5").innerText = jsonObject.totalAll;
							}
						}
					}  


 
 
<pre name="code" class="html">JSP页面显示代码:
<span id="th1"></span><pre name="code" class="html"><span id="th2"></span><pre name="code" class="html"><span id="th3"></span><pre name="code" class="html"><span id="th4"></span><pre name="code" class="html"><span id="th5"></span>
 
 
 
 


 
 
 
 
 


望加指点。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值