Datagrid在页面中不显示数据解决过程

    初学Datagrid,遇到许多问题,而且大都没有在网上找到解决方法,不过在同事的帮助下最终还是成功的解决了问题。

    之前使用Dao,已完成一个能实现增删改查功能的试卷生成系统,但是被老板嫌弃表格“太土”,如下所示,所以是在这个基础上使用Datagrid改变表格的呈现形式。


1.我的主要问题是生成的json格式有问题(jsp文件生成的json的格式是对的,但是对于页面显示来说是有问题的)。

最初的jsp文件为:

  <%
List<Question> list = DAOFactory.getIEmpDAOInstance().selectAllQuestion();         //之前自己写的方法
List<String> dateList1 = new ArrayList<String>();

List<String> dateList2 = new ArrayList<String>();

for(int i=0;i<list.size();i++) {
Question p = list.get(i);
dateList1.add(p.getP_id());
dateList2.add(p.getTitle());

}

Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.put("p_id", dateList1);
        dataMap.put("title", dateList2);
        Gson gson = new Gson();
        String s = gson.toJson(dataMap);
        out.write(s);   
    %>

jsp文件运行后,生成的json为:

{"title":["该教师是否关爱学生?","该教师有没有对你所学的科目及时辅导复习?","该教师上课是否使用普通话?","该教师是否在课间时主动与你交流?","你对该教师布置及批改作业的情况是否满意?"],"p_id":["1","2","3","4","5"]}

使用在线json格式校验工具校验后,发现生成的json格式正确,但是页面里就是显示不出数据:



然后,更改jsp文件,主要是更改了生成json文件的代码:

<%
List<Question> list = DAOFactory.getIEmpDAOInstance().selectAllQuestion();
Map<String, Object> mapAll = new HashMap<String, Object>();
List<Map<String, Object>> list2 = new ArrayList<Map<String, Object>>();

for(int i=0;i<list.size();i++) {
Question p = list.get(i);

Map<String, Object> map = new HashMap<String, Object>();
                map.put("p_id",p.getP_id());
                map.put("title", p.getTitle());
                list2.add(map);

}

 mapAll.put("total", 5);
          mapAll.put("rows", list2);
          Gson gson = new Gson();
          String s = gson.toJson(mapAll);
          out.write(s);

%>

jsp文件运行后,生成的json为:

{"total":5,

 "rows":[{"title":"该教师是否关爱学生?","p_id":"1"},

             {"title":"该教师有没有对你所学的科目及时辅导复习?","p_id":"2"},

             {"title":"该教师上课是否使用普通话?","p_id":"3"},

             {"title":"该教师是否在课间时主动与你交流?","p_id":"4"},

             {"title":"你对该教师布置及批改作业的情况是否满意?","p_id":"5"}]}

更改后,页面显示结果:


问题解决!

2.如果你的问题还是没有解决,可以检查一下你的jsp文件的格式,我之前直接使用了eclipse生成的jsp文件,然后就是怎样都显示不出来,后来jsp文件被同事删的只剩下这些:

<%@ page language="java" pageEncoding="UTF-8"%>
<%@page import = "com.eshore.factory.DAOFactory" %>  //自己创建的相关包
<%@page import = "com.eshore.pojo.Question" %>          //自己创建的相关包
<%@page import = "java.util.*" %>

<%@page import="com.google.gson.Gson"%>
<%@page import="java.math.BigDecimal"%>
<%@page import="java.sql.ResultSet"%>

<%
List<Question> list = DAOFactory.getIEmpDAOInstance().selectAllQuestion();
Map<String, Object> mapAll = new HashMap<String, Object>();
List<Map<String, Object>> list2 = new ArrayList<Map<String, Object>>();

for(int i=0;i<list.size();i++) {
Question p = list.get(i);

Map<String, Object> map = new HashMap<String, Object>();
                map.put("p_id",p.getP_id());
                map.put("title", p.getTitle());
                list2.add(map);
}
 mapAll.put("total", 5);
          mapAll.put("rows", list2);
          Gson gson = new Gson();
          String s = gson.toJson(mapAll);
          out.write(s);
%>

然后就显示出来了。


猜你喜欢

转载自blog.csdn.net/weixin_40161907/article/details/80268583