现在的位置: 首页 > 综合 > 正文

AJax 返回 json

2013年05月06日 ⁄ 综合 ⁄ 共 2198字 ⁄ 字号 评论关闭

                在最近的项目中遇到这样一个问题:用户点击一个按钮后要调用后台的方法,此方法将返回一个List的集合;页面要求,不刷新整个页面只刷新局部页面,并将List中的对象遍历的显示在页面中。

局部刷新,我最快想到的都是用Ajax来完成,但是之前没有用过json,所以在实现的过程中遇到了一点小麻烦,不过最后还是解决了。

页面代码为:

<div>您这天作试卷了吗?
	<span class="len_yes" onclick="checkExamYes('<s:property value="checkDay"/>');"><a href="javascript:void(0)">做了</a></span>
</div>

<script type="text/javascript">
 
      function checkExamYes(checkDay){
 	    $.ajax({
			url : "<%=contextPath%>/stu/studyplanWeb!getExamSummaryLimtContent.action",
			type : "post",
			dataType : "json",
			data : {
				"examSummary.summarydate" : checkDay
			},
			success : function(result) {
				//显示 
				var epNameList = result.entity;
				if(epNameList == null){
					checkExam(checkDay);
				}else{
					var displayexminfo="";	
                                         //用Jquery的each()方法循环遍历得到的epNameList
					 $.each(epNameList,function(key,val){
					 	
						displayexminfo+="<li>您做了"+"<font style='color:yellow;'>"+ val.substring(0,6)+"..."+"</font>"+"试卷。</li>";
						
						//alert(key); (只显示5条计入) 
     							if(key=='5'){            
				            return false; //跳出循环  
				         }
					 });
					 
						$("#examdo2").show();
						$("#examdo").hide();
						$("#resExamText").html(displayexminfo);
						$("#resExamText").show();
				}
			},
			error : function(error) {
				showErrorWin('error===checkExamYes','');
		   }
	    });
     }
 
 
</script>

java 代码为:

    public String getExamSummaryLimtContent(){ 
    	List<ExamSummary> examSummaryList = null;
    	try{
    		QueryExamSummaryCondition qExamsummaryCondition = getQueryExamSummaryCondition();
        	
    		// 当天
    		String currdate = DateUtil. formatDate(examSummary.getSummarydate());
    		Date summarydate = DateUtil.toSqlDate(currdate, "yyyy-MM-dd");		
        	qExamsummaryCondition.setSummarydate(summarydate);
        	qExamsummaryCondition.setCusId(getLoginUserId());
        	
        	examSummaryList =  examSummaryService.getExamSummaryList(queryExamSummaryCondition);
        	
        	List<String>  epNameList = new ArrayList<String>();
        	if(examSummaryList != null && examSummaryList.size()>0){
        		//取得 Exapa的epName 集合
        		for(int i=0;i<examSummaryList.size();i++){
        			ExamSummary es = examSummaryList.get(i);
        			int epId = es.getExamId();
        			Exapa exapa = null;
        			
        			exapa = examSummaryService.getExampaperById(epId);
        			System.out.println(exapa.getEpName());
        			if(exapa != null){
        				String epName = exapa.getEpName();
        				epNameList.add(epName);
        			}
        		}
        		//将epNameList设置进Result中
        		setResult( new Result<List<String>>(false,"success",null,epNameList));
        	}else{
    			setResult( new Result<List<String>>(false,"failure",null,null));
    		}
        	
    	}catch(Exception e){
    		e.printStackTrace();
    		setResult( new Result<List<String>>(false,"failure",null,null));
    	
    	}
    	
    	return "json" ;
    }

抱歉!评论已关闭.