0

我创建了一个 jsonarray 并从 servlet 发送,并发送到前端,前端应该得到消息和 currentTime,并在网站上显示它们。我应该如何在 EXTjs 中做到这一点:
这是 serverlet 中的代码:

public void loadHistory(HttpServletRequest request, HttpServletResponse response) throws IOException{

        JsonObject json = new JsonObject();
        JsonArray dataArray = new JsonArray();
        String groupName = request.getParameter("groupName");
        String chatRoomName = getChatRoom(groupName);

        Database db = new Database(chatRoomName);
        CouchDbClient dbClient = db.getDbClient();

        PrintWriter out = response.getWriter();


        int i=0;
        while (dbClient.contains(String.valueOf(i++))){
            JsonObject objHistory = dbClient.find(JsonObject.class, String.valueOf(i++));
            String preMessage = objHistory.get("message").getAsString();
            String preTime = objHistory.get("currentTime").getAsString();
            json.addProperty("message", preMessage);
            json.addProperty("currentTime", preTime);
            dataArray.add(json);
        }
        if (dataArray!=null){
            response.setContentType("application/json");
            out.print(dataArray);
            out.close();
        }
    }
4

1 回答 1

0

在发送响应之前修改 if 条件中的以下代码。

  if (dataArray!=null){
         response.setContentType("application/json");
         JSONObject resultJsonMain=new JSONObject();
         resultJsonMain.put("resultJsonMain",dataArray);
         out.println(resultJsonMain);

        out.close();
    }
于 2013-10-28T10:13:59.263 回答