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

jquery调用struts2,返回script/text/json格式的数据

2012年10月04日 ⁄ 综合 ⁄ 共 3835字 ⁄ 字号 评论关闭

本文在ajax中调用struts2 action ,查询数据库,然后返回字符串,演示返回script,text,json类型的数据的用法 

一。返回script和text时代码都一样 

Java代码  收藏代码
  1. ajax-jquery.js  
  2. function commonAjax(oper,prod,url){  
  3.     oper.bind("change",function(){comJquery(oper,prod,url)});  
  4.     prod.bind("change",function(){comJquery(oper,prod,url)});  
  5. }  
  6. function comJquery(oper,prod,url){  
  7.             var prodId=prod.val();  
  8.             if(oper.val()!=''&&prod.val()!=''&&prod.val()!=0&&prod.val()!=-1){  
  9.                 jQuery.ajax({  
  10.                     url : url,  
  11.                     data : {productId : prodId},  
  12.                     type : "post",  
  13.                     cache : false,  
  14.                     dataType : "script"或者"text",  
  15.                     success:callback  
  16.                     });  
  17.             }else{  
  18.                 $("#company").html('');  
  19.             }  
  20. }  
  21.   
  22. function callback(data){   
  23.     $("#company").html(data);  
  24. }  
  25.   
  26.   
  27. jsp页面调用ajax js(不管返回什么类型,调用的代码都一样)  
  28. <script type="text/javascript" src="script/jquery.js"></script>  
  29. <script type="text/javascript" src="script/json2.js"></script>  
  30. <script type="text/javascript" src="script/ajax-jquery.js"></script>  
  31.   
  32. var op=$("#cbApplySubmit_changeApplyFormBO_operationId");  
  33. var pr=$("#cbApplySubmit_changeApplyFormBO_productId");     
  34. var url="${contextPath}/assets/businessChange/ajaxGetCompany.do";   
  35. commonAjax(op,pr,url);     
  36.   
  37.   
  38.   
  39. struts2 action  
  40.   
  41. private Integer productId;  
  42. private IProductMng productMng; // 通过spring注入的service  
  43.   
  44. // get set  
  45.   
  46. public void ajaxGetCompany() throws Exception {  
  47.         ProductBO prod = productMng.loadProduct(productId);  
  48.         Integer companyId = prod.getCompanyId();  
  49.         CompanyBO comp = productMng.loadCompany(companyId);  
  50.         String message = "事业部为:" + comp.getName();  
  51.         sendMsg(message);  
  52.     }  
  53.       
  54.     public void sendMsg(String content) throws IOException{      
  55.         HttpServletResponse response = ServletActionContext.getResponse();      
  56.         response.setCharacterEncoding("UTF-8");      
  57.         response.getWriter().write(content);      
  58.     }      

二。返回json格式 

Java代码  收藏代码
  1. function commonAjax(oper,prod,url){  
  2.     oper.bind("change",function(){comJquery(oper,prod,url)});  
  3.     prod.bind("change",function(){comJquery(oper,prod,url)});  
  4. }  
  5. function comJquery(oper,prod,url){  
  6.             var prodId=prod.val();  
  7.             if(oper.val()!=''&&prod.val()!=''&&prod.val()!=0&&prod.val()!=-1){  
  8.                 jQuery.ajax({  
  9.                     url : url,  
  10.                     data : {productId : prodId},  
  11.                     type : "post",  
  12.                     cache : false,  
  13.                     dataType : "json",  
  14.                     success:callback  
  15.                     });  
  16.             }else{  
  17.                 $("#company").html('');  
  18.             }  
  19. }  
  20.   
  21. function callback(data){   
  22.     $("#company").html(data[0].msg);  // 因为json对象中只有一个值  
  23. // 完整写法如下  
  24. //function callback(data){  
  25. //  var buf="";  
  26. //  for(var i=0;i<data.length;i++){  
  27. //      buf+=data[i].msg;  
  28. //  }  
  29. //  $("#company").html(buf);  
  30. //}   
  31. }   
  32.   
  33.   
  34.   
  35. struts2 action 需要组装json对象  
  36. import net.sf.json.JSONArray;  
  37.   
  38.     public void ajaxGetCompany() throws Exception {  
  39.         ProductBO prod = productMng.loadProduct(productId);  
  40.         Integer companyId = prod.getCompanyId();  
  41.         CompanyBO comp = productMng.loadCompany(companyId);  
  42.         String message = "事业部为:" + comp.getName();  
  43.         JSONArray jsonObj = JSONArray.fromObject("[{msg:'" + message +"'}]");  
  44.         sendMsg(jsonObj.toString());  
  45.     }  
  46.       
  47.     public void sendMsg(String content) throws IOException{      
  48.         HttpServletResponse response = ServletActionContext.getResponse();      
  49.         response.setCharacterEncoding("UTF-8");      
  50.         response.getWriter().write(content);      
  51.     }      


jquery.js.rar (28.4
KB)

json2.js.rar (5.2
KB)

ajax-jquery.js.rar (441
Bytes)

抱歉!评论已关闭.