zoukankan      html  css  js  c++  java
  • 前后台数据传递

    一:后台JSON,前端AJAX
    (一)AJAX中后台JSON的转化
    (1)返回数组:定义JSONArray jsonArray使用jsonArray.add(String string)方法直接添加,然后调用jsonArray.toJSONString()返回给前端。
    (2)返回对象数组:(由于尖括号会被解释掉,所以下面用()代替)
    方法①:定义List (Map (String, Object))data 里面存放 Map (String, Object); 然后遍历对象的集合,循环中定义Map(String, Object) tempMap;调用tempMap.put(object.para,object.value);然后data.add(tempMap);循环结束后,调用String listJson = JSON.toJSONString(data);
    pw.write(listJson.toString());直接返回

    List< Map< String, Object > > data = new ArrayList<Map<String, Object>>();
    		
    		for(int i=0;i<questionList.size();i++){   //questionList为存放对象的List
    			Map<String, Object> m=new HashMap<>();
    			m.put("id", questionList.get(i).getId());
    			m.put("answerA", questionList.get(i).getAnswerA());
    			m.put("answerB", questionList.get(i).getAnswerB());
    			m.put("answerC", questionList.get(i).getAnswerC());
    			m.put("answerD", questionList.get(i).getAnswerD());
    			m.put("question", questionList.get(i).getQuestion());
    			data.add(m);
    		}
    		
    		String listJson = JSON.toJSONString(data);
    		 pw.write(listJson.toString());
             pw.flush();
             pw.close();
    
    

    方法②:定义一个JSONArray jsonArray。然后遍历对象集合,循环中定义一个JSONObject jsonObject调用jsonObject.put(object.para,object.value)。jsonArray.add(jsonObject)然后调用循环结束后调用String listJson = JSON.toJSONString(jsonArray)返回

        JSONArray jsonArray =new JSONArray ();
    		for(int i=0;i<questionList.size();i++){
    			JSONObject jsonObject =new JSONObject ();
    			
    			jsonObject.put("id", questionList.get(i).getId());
    			jsonObject.put("answerA", questionList.get(i).getAnswerA());
    			jsonObject.put("answerB", questionList.get(i).getAnswerB());
    			jsonObject.put("answerC", questionList.get(i).getAnswerC());
    			jsonObject.put("answerD", questionList.get(i).getAnswerD());
    			jsonObject.put("question", questionList.get(i).getQuestion());
    			jsonArray.add(jsonObject);
    		}
           String listJson = JSON.toJSONString(jsonArray);
    		    pw.write(listJson.toString());
                         pw.flush();
                         pw.close();
    

    (二)前端接收正常的AJAX的success:function(result){]

    二:后台Java 对象 ,前端el表达(c标签)

    (一)传一个Java对象

    //后台
    User u= new User(i,"姓名"+i,"密码"+i,"123","10");
    req.setAttribute("user", u);
    
    //前端
    <tr>
          <td>${user.userName}</td>     
          <td>${user.password }</td>     
          <td>${user.phoneNumber }</td>
          <td>${user.userMail }</td>
        </tr>
    

    (二)传多个对象-->对象List

    //后台
    List<User>userList=new ArrayList<>();
    		for(int i=0;i<2;i++){
    			User u= new User(i,"姓名"+i,"密码"+i,"123","10");
    			userList.add(u);
    		}
    		req.setAttribute("userList", userList);
    
    //前端
    <c:forEach items="${requestScope.userList}" var="keyword"> 
        <tr>
          <td>${keyword.userName}</td>     
          <td>${keyword.password }</td>     
          <td>${keyword.phoneNumber }</td>
          <td>${keyword.userMail }</td>
        </tr>
    </c:forEach>
    
  • 相关阅读:
    pandas中的时间序列基础
    Python中的进程
    Pandas透视表和交叉表
    Pandas分组级运算和转换
    Python中的线程详解
    Pandas聚合
    Python面试题整理
    Pandas分组
    暑假集训 || 动态规划
    DFS || HDU 2181
  • 原文地址:https://www.cnblogs.com/hts-technology/p/8041914.html
Copyright © 2011-2022 走看看