使用java的数据服务器端处理

时间:2021-07-06 14:22:17

I am using jquery datatable for my project. It's working well when i have less columns. But when it goes above 18 columns,it refuses to apply sorting and colvis functionalities. My current code is as below.

我正在为我的项目使用jquery数据表。当我的列数较少时,它运行良好。但当它超过18列时,它拒绝应用排序和colvis功能。我目前的代码如下。

<table id="testTable" class="display nowrap stripe row-border order-column" cellspacing="0" align="center">
<%
    try {
      String a="";
      String acode = "N/A";
      int atp1, id4;
      query = "SELECT * FROM IETMS;";
      stmt = conn.createStatement();
      rs = stmt.executeQuery(query);
      ResultSetMetaData rsmd=rs.getMetaData();
      int columns = rsmd.getColumnCount();
      if(rs.next()) {
%>
  <thead>
    <tr style="background-color: #3f91bd;color:white;">
      <% for (i=1; i<=columns-2; i++) {%>
        <th><%= rsmd.getColumnLabel(i) %></th>
      <%  } %>
    </tr>
  </thead>
  <tfoot>
    <tr class="header">
      <% for (i=1; i<=columns-2; i++) {%>
      <th><%= rsmd.getColumnLabel(i) %></th>
      <%  }%>
    </tr>
  </tfoot>
<%}%>
<tbody stye="border: 2px solid green;">
  <% while(rs.next()){ %>
    <tr class="clickable">
    <%
      for (i=1; i<=columns-2; i++) {
        if(rs.getObject(i) == null)
        {
          a = "N/A";
        } else
          a=""+rs.getObject(i);
    %>
    <td height="15"><%=a%></td>
    <% }%>
    </tr>
  <%}%>
</tbody>
<%
  } catch (Exception ex) {
      out.println(ex);
  }
%>
</table>

I'd like to move the implementation to a new JSP page that fetches data using JSON, but I'm not really all that familiar with JSON. I've looked at many examples, but it seems like JSON has a fixed schema, and I want to dynamically display whatever data is returned. Is there a way to do this with JSON?

我想将实现移动到一个使用JSON获取数据的新JSP页面,但我并不是那么熟悉JSON。我看了很多例子,但似乎JSON有一个固定的模式,我想动态显示返回的数据。有没有办法用JSON做到这一点?

1 个解决方案

#1


1  

You can dynamically display JSON data, but it's not that easy and it's not necessarily tabular in nature. If you are certain that it follows a particular pattern -- say, an array of objects with no nested objects and each object has the same properties, then I suppose you could display it in a table format by iterating over the array and then iterating across each o the properties in the objects, displaying each property in a column:

您可以动态显示JSON数据,但这并不容易,并且它本质上不一定是表格。如果您确定它遵循特定模式 - 比如,没有嵌套对象的对象数组,并且每个对象具有相同的属性,那么我想您可以通过遍历数组然后迭代遍历表格格式来显示它每个对象中的属性,显示列中的每个属性:

Here's a piece of sample code using the Jackson API for instance -- it's not a JSP, but it's not hard to use roughly the same logic in JSP:

这是一段使用Jackson API的示例代码 - 它不是JSP,但在JSP中使用大致相同的逻辑并不难:

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;

public class JsonTable {
    JsonParser parser;

    public JsonTable( String filename ) {
        try {
            JsonFactory f = new JsonFactory();
            InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream( filename );
            parser = f.createParser(stream);
        } catch ( IOException e ) {
            e.printStackTrace();
        }
    }

    public String getTable() throws Exception {
        try {
            StringBuilder builder = new StringBuilder();
            List<String> header = new ArrayList<>();
            if ( parser.nextToken() != JsonToken.START_ARRAY ) {
                throw new RuntimeException( "Expected an array of objects." );
            }
            while ( parser.nextToken().equals(JsonToken.START_OBJECT) ) {
                buildRow(header,builder);
            }
            if ( parser.currentToken() != JsonToken.END_ARRAY ) {
                throw new RuntimeException( "Expected end of JSON array, got: " + parser.currentToken() );
            }
            wrapRowsWithTable(header,builder);
            return builder.toString();
        } finally {
            parser.close();
        }
    }

    private void wrapRowsWithTable( List<String> columns, StringBuilder builder ) {
        StringBuilder header = new StringBuilder();
        header.append("<table><thead><tr>");
        for( String column : columns ) {
            header.append("<th>" + column + "</th>");
        }
        header.append("</tr></thead>");
        builder.insert(0,header.toString());
        builder.append("</table>");
    }

    private void buildRow( List<String> header, StringBuilder builder ) throws IOException {
        int index = 0;
        builder.append("<tr>");
        while(parser.nextToken() != JsonToken.END_OBJECT)
        switch( parser.currentToken() ) {
        case FIELD_NAME:
            if( header.size() > index ) {
                if( !header.get(index).equals( parser.getCurrentName() ) ) {
                    throw new RuntimeException( "Expected field: " + header.get(index) + ", found: " + parser.getCurrentName() );
                }
            } else {
                header.add(parser.getCurrentName());
            }
            index++;
            break;
        case VALUE_NULL:
        case VALUE_TRUE:
        case VALUE_FALSE:
        case VALUE_NUMBER_INT:
        case VALUE_NUMBER_FLOAT:
        case VALUE_STRING:
            builder.append("<td>" + parser.getText() + "</td>");
            break;
        case VALUE_EMBEDDED_OBJECT:
            throw new RuntimeException( "Expecting an array of objects with no nested objects." );
        default:
            throw new RuntimeException( "Unexpected token: " + parser.currentToken() );
        }
    }
}

This expects the fields to follow the same order, which might not work for every scenario -- you could enhance it to allow fields to appear in any order and/or be optional.

这要求字段遵循相同的顺序,这可能不适用于每个方案 - 您可以增强它以允许字段以任何顺序出现和/或是可选的。

If the data is a JSON object tree or some other kind of pattern, it's not going to be easy to represent that as a table.

如果数据是JSON对象树或某种其他类型的模式,那么将其表示为表格并不容易。

#1


1  

You can dynamically display JSON data, but it's not that easy and it's not necessarily tabular in nature. If you are certain that it follows a particular pattern -- say, an array of objects with no nested objects and each object has the same properties, then I suppose you could display it in a table format by iterating over the array and then iterating across each o the properties in the objects, displaying each property in a column:

您可以动态显示JSON数据,但这并不容易,并且它本质上不一定是表格。如果您确定它遵循特定模式 - 比如,没有嵌套对象的对象数组,并且每个对象具有相同的属性,那么我想您可以通过遍历数组然后迭代遍历表格格式来显示它每个对象中的属性,显示列中的每个属性:

Here's a piece of sample code using the Jackson API for instance -- it's not a JSP, but it's not hard to use roughly the same logic in JSP:

这是一段使用Jackson API的示例代码 - 它不是JSP,但在JSP中使用大致相同的逻辑并不难:

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;

public class JsonTable {
    JsonParser parser;

    public JsonTable( String filename ) {
        try {
            JsonFactory f = new JsonFactory();
            InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream( filename );
            parser = f.createParser(stream);
        } catch ( IOException e ) {
            e.printStackTrace();
        }
    }

    public String getTable() throws Exception {
        try {
            StringBuilder builder = new StringBuilder();
            List<String> header = new ArrayList<>();
            if ( parser.nextToken() != JsonToken.START_ARRAY ) {
                throw new RuntimeException( "Expected an array of objects." );
            }
            while ( parser.nextToken().equals(JsonToken.START_OBJECT) ) {
                buildRow(header,builder);
            }
            if ( parser.currentToken() != JsonToken.END_ARRAY ) {
                throw new RuntimeException( "Expected end of JSON array, got: " + parser.currentToken() );
            }
            wrapRowsWithTable(header,builder);
            return builder.toString();
        } finally {
            parser.close();
        }
    }

    private void wrapRowsWithTable( List<String> columns, StringBuilder builder ) {
        StringBuilder header = new StringBuilder();
        header.append("<table><thead><tr>");
        for( String column : columns ) {
            header.append("<th>" + column + "</th>");
        }
        header.append("</tr></thead>");
        builder.insert(0,header.toString());
        builder.append("</table>");
    }

    private void buildRow( List<String> header, StringBuilder builder ) throws IOException {
        int index = 0;
        builder.append("<tr>");
        while(parser.nextToken() != JsonToken.END_OBJECT)
        switch( parser.currentToken() ) {
        case FIELD_NAME:
            if( header.size() > index ) {
                if( !header.get(index).equals( parser.getCurrentName() ) ) {
                    throw new RuntimeException( "Expected field: " + header.get(index) + ", found: " + parser.getCurrentName() );
                }
            } else {
                header.add(parser.getCurrentName());
            }
            index++;
            break;
        case VALUE_NULL:
        case VALUE_TRUE:
        case VALUE_FALSE:
        case VALUE_NUMBER_INT:
        case VALUE_NUMBER_FLOAT:
        case VALUE_STRING:
            builder.append("<td>" + parser.getText() + "</td>");
            break;
        case VALUE_EMBEDDED_OBJECT:
            throw new RuntimeException( "Expecting an array of objects with no nested objects." );
        default:
            throw new RuntimeException( "Unexpected token: " + parser.currentToken() );
        }
    }
}

This expects the fields to follow the same order, which might not work for every scenario -- you could enhance it to allow fields to appear in any order and/or be optional.

这要求字段遵循相同的顺序,这可能不适用于每个方案 - 您可以增强它以允许字段以任何顺序出现和/或是可选的。

If the data is a JSON object tree or some other kind of pattern, it's not going to be easy to represent that as a table.

如果数据是JSON对象树或某种其他类型的模式,那么将其表示为表格并不容易。