简体   繁体   English

Json Data如何使用Jsp传递到DataTable

[英]Json data how to pass in to DataTable using Jsp

i am creating simple crud system using jsp. 我正在使用jsp创建简单的crud系统。 data passing through json format i tested through console it is passing well but data is not display on the datatable.i don't why. 我通过控制台测试过的通过json格式的数据传递得很好,但是数据没有显示在数据表上。我不为什么。 i wrote what i tried so far. 我写了到目前为止我尝试过的东西。

Table

<table id="tbl-projects" class="table table-responsive table-bordered" cellspacing="0" width="100%">
    <thead>
        <tr>
            <th></th>
            <th></th>
            <th></th>
            <th></th>
            <th></th>
        </tr>
    </thead>    
 </table>

jQuery jQuery的

<script>
get_all()
 function get_all()
 {
     $('#tbl-projects').dataTable().fnDestroy();
     $('#etbl-projects').DataTable().ajax.reload();
     $.ajax({
         url : "all_project.jsp",
         type : "GET",
         dataType : "JSON",
         success:function(data)
         {      
             alert(data.course);
             $('#tbl-projects').dataTable({
                 "aaData": data,
                 "scrollX": true,
                 "aoColumns": [
                     {"sTitle": "StudentName", "mData": "name"},
                     {"sTitle": "Course", "mData": "course"},
                     {"sTitle": "Fee", "mData": "fee"},                
                     {
                         "sTitle": "Edit",
                         "mData": "id",
                         "render": function (mData, type, row, meta) {
                             return '<button class="btn btn-xs btn-success" onclick="get_project_details(' + mData + ')">Edit</button>';
                          }
                     },
                     {
                         "sTitle": "Delete",
                         "mData": "id",
                         "render": function (mData, type, row, meta) {
                             return '<button class="btn btn-xs btn-primary" onclick="Remove_details(' +  mData + ')">Delete</button>';
                          }
                     }
                 ]
             });

         },
         error: function (xhr, status, error) {
             alert(xhr);
             console.log(xhr.responseText);


             $('#save').prop('disabled', false);
             $('#save').html('');
             $('#save').append('Save');
         }
     });
 }

</script>

all_project.jsp Jsp Page all_project.jsp Jsp页面

<%@page import="org.json.simple.JSONObject"%>
<% Class.forName("com.mysql.jdbc.Driver"); %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %>
<%
    Connection con;
    PreparedStatement pst;
    ResultSet rs;
    Class.forName("com.mysql.jdbc.Driver");
    con = DriverManager.getConnection("jdbc:mysql://localhost/studcrud", "root","");
    String query="select * from records";  
    Statement stmt=con.createStatement();
    rs=stmt.executeQuery(query);
    while(rs.next())
    {    
        String id   =rs.getString("id");
        String name   =rs.getString("name");
        String course   =rs.getString("course");
        String fee   =rs.getString("fee"); 

        JSONObject json = new JSONObject();
        json.put("name", name);
        json.put("course", course);
        json.put("fee", fee);
        json.put("id", id);
        out.print(json);
        out.flush();      

    }

%>

i have tested through the console result. 我已经通过控制台结果进行了测试。 screen shot below 屏幕截图如下

{"fee":"10000","name":"john","course":"java","id":"1"}{"fee":"7000","name":"Raja","course":"C#","id":"2"}{"fee":"2323","name":"sad","course":"asd","id":"3"}{"fee":"12000","name":"Nishan","course":"Jsp","id":"4"}

i have attached the Datatable image below 我已经在下面附加了数据表图像

enter image description here 在此处输入图片说明

Your response in JSON format is invalid (missing comas between object). 您的JSON格式响应无效(对象之间缺少逗号)。

Use org.json.simple.JSONArray to create a list of objects, for example: 使用org.json.simple.JSONArray创建对象列表,例如:

<%@page import="org.json.simple.JSONArray"%>

...

JSONArray list = new JSONArray();

while(rs.next())
{    
    String id     = rs.getString("id");
    String name   = rs.getString("name");
    String course = rs.getString("course");
    String fee    = rs.getString("fee"); 

    JSONObject obj = new JSONObject();
    obj.put("name", name);
    obj.put("course", course);
    obj.put("fee", fee);
    obj.put("id", id);

    list.put(obj);
}

out.print(list.toJSONString());
out.flush();      

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM