12-10每日博客

今天记录一下我在使用layui组件中的表格的时候遇到的问题,就是如何将MySQL读取到的数据显示在表格,组件中的url指向的就可以是后台的servlet,然后servlet向前台传递的是一个json数据,并且要按照官网中的案例进行返回,不然前台的界面无法加载数据并且会提示接口异常。这里的JSON是

net.sf.json.JSONArray;
net.sf.json.JSONObject;
这两个是用来传输网络数据的,我的一个方法如下:
public void PopTable( HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException, SQLException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=UTF-8");
String tabname=request.getParameter("tabname");
List<SqlTableBean> list=dao.getTableAll(tabname);
JSONArray json=new JSONArray();
for(int i=0;i<list.size();i++){
JSONObject ob=new JSONObject();
ob.put("ywname",list.get(i).getYwname());
ob.put("zwname",list.get(i).getZwname());
ob.put("danwei",list.get(i).getDanwei());
ob.put("zdtype",list.get(i).getZdtype());
ob.put("beizhu",list.get(i).getBeizhu());
ob.put("ispri",list.get(i).getIspri());
ob.put("chtime",list.get(i).getChtime());
json.add(ob);
}
JSONObject ob=new JSONObject();
ob.put("code", 0);
ob.put("msg", "");
ob.put("count",1);
ob.put("data",json);
PrintWriter out = response.getWriter();
out.write(ob.toString());
}
其中的这几个是官网中要求必须返回的,这样写前台的表格就可以正常显示了。
ob.put("code", 0);
ob.put("msg", "");
ob.put("count",1);
ob.put("data",json);

posted @ 2021-12-10 22:00  软工新人  阅读(38)  评论(0)    收藏  举报