標籤:cte org character hash function flush tran object 前端
其實這種方式已經很少用了,因為我們已經有很成熟的架構可以用,例如stringmvc+hibernate 它們對這些互動有一些很成熟的封裝。
但是當不用架構時,有些東西還是需要知道的。
1.在後台代碼中傳遞參數,放入流中。
@RequestMapping(value="test")public void jjj(HttpServletResponse response,int s,int d){ try { response.setCharacterEncoding("utf-8"); PrintWriter pw=response.getWriter(); List<Student> list=studentservice.getAllStudentByTeaId("1", 1); HashMap map=new HashMap(); HashMap map1=new HashMap(); map.put("list",list); map.put("count", 123); map.put("count1", 123); map.put("list1", list); JSONArray array=JSONArray.fromObject(map); pw.write(array.toString()); pw.flush(); pw.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }
2.在前端的擷取中,需要jquery.js
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Insert title here</title><script type="text/javascript" src="js/jquery.js"></script><script type="text/javascript">function btn(){ $.ajax({ url:"test", type:"post", data:{ ‘s‘:1, ‘d‘:2 }, cache:false, dataType:"json", success:function(data){ for(var key in data){ alert(data[key].count); alert(data[key].count1); $.each(data[key].list,function(index,item){ $(‘#span‘).html($(‘#span‘).html()+" 學號 : "+item.id+" 姓名 :"+item.name); }); alert(data[key].list1); } } });}</script></head><body><span id="span"></span><input type="button" value="點擊" onclick="btn()"></body></html>
java後台傳遞多個對象到ajax中,簡單的放入,傳遞與遍曆