2

I have a servlet that returns a collection of model bean objects to a jsp page. From there I want to display that data, but I'm not sure of the correct way to do it.

The average number of rows returned from the database will probably be less than a few hundred at best.

I'm using Java 1.6 and the default jQuery with Datatables 1.10.4.

I did verify that the data is in the collection and that it goes out in the request to the JSP page.

Originally, I had hard coded values for the data to make sure I had the datatable setup properly, based on Javascript Sourced Data from the Datatables site, but now I have actual data and I'm not sure how to get it in the table properly.

Here is my new jQuery code that is showing this error here in Eclipse:

 "ajax': './MyServlet", //invalid property assignment error?

I'm not sure what that error means?

 <!-- jQuery code -->
     $(document).ready(function () {
            $('#datatable').dataTable({
                "processing": true,
                "ajax': './MyServlet", 
                "columns": [
                { "data": "Last Name" },
                { "data": "First Name" },
                { "data": "Mailing Address" },
                ...
            ]
            });
        }); 

Here is my HTML datatable code with scriptlets, for now to loop through my collection of objects and add in columns/rows. I saw this in another example, so I used it just trying to get it working.

<!-- The datatable in the jsp page -->
<table width="100%" id="datatable" class="display compact" cellspacing="0">
        <%
            if(null != modelBeanCollection  && modelBeanCollection.size() > 0) {
        %>

        <%
            List<modelBean> projectList = (List<modelBean>)(List<?>) modelBeanCollection;
            for (modelBean project : projectList) { 
        %>
        <tr>
            <td><%=project.getLastName()%></td>
            <td><%=project.getFirstName()%></td>
            <td><%=project.getAddress1()%></td>
            ...
        </tr>
            <%}
        }%>

</table>

My main question is, what is the best practice to display an array of objects in a jsp page, with what I have, using datatables, based on a few hundred rows of data?

1
  • 1
    "ajax': './MyServlet" you have single quotes in here Commented Apr 30, 2015 at 5:40

1 Answer 1

2

By looking at your code, you are trying to initale the datatables with an ajax call and at the same time with the whole data from jsp scriptlet.

In your case I don't think the ajax part is needed. Here is what I'd do;

<table id="example" class="display" cellspacing="0" width="100%">
    <thead>
        <tr>
            /* Column names goes here for header */
            <th>A</th>
            <th>B</th>
               ....
        </tr>
    </thead>

  <tbody>
     <%
        if(null != modelBeanCollection  && modelBeanCollection.size() > 0) {
    %>

    <%
        List<modelBean> projectList = (List<modelBean>)(List<?>) modelBeanCollection;
        for (modelBean project : projectList) { 
    %>
    <tr>
        <td><%=project.getLastName()%></td>
        <td><%=project.getFirstName()%></td>
        <td><%=project.getAddress1()%></td>
        ...
    </tr>
        <%}
    }%>
  </tbody>
</table>

And at the $(document).ready event just a simple initialize statement like this;

$('#example').dataTable( {
    "processing": true
} );

And you are set!

Also I'd like to suggest you to use EL for JSP pages, it'll save you a lot of headaches and it's so much easier to use. The above scriptlet could be written just like this:

    <c:if test="{not empty modelBeanCollection}">
      <c:forEach items="${modelBeanCollection}" var="project">
        <tr>
              <td>${project.getLastName}></td>
              <td>${project.getFirstName}></td>
              <td>${project.getAddress1}</td>
              ...
          </tr>
      </c:forEach>
    </c:if>
Sign up to request clarification or add additional context in comments.

1 Comment

Yes I will use JSTL, but I'm just trying to get it working. This fixes my syntax issue, but I have to fix my Collection to a list cast exception now. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.