0

I have a couple of ArrayLists with variable length and sometimes null. This ArrayList contains a bunch of objects. The table should have columns based on (some) attributes of the object. And the table should be displayed on a jsp.

I have two ideas, one is to use a JSTL tag the other is to use JavaScript. And library suggestions are welcome.

2
  • You're confusing JavaScript with JSP scriptlets. Commented Dec 14, 2010 at 17:47
  • No actually I was referring to JavaScript, but Stephen P (below) has already clarified that JavaScript does not have access to Java Objects. Commented Dec 14, 2010 at 21:21

3 Answers 3

1

JSTL is the standard, preferred way (unless you need to load it via ajax, for example)

<table>
<tr><td>Foo header</td><td>Bar header</td></tr>
<c:forEach items="${yourRequestScopedArrayList}" var="obj">
    <tr>
       <td>${obj.foo}</td>
       <td>${obj.bar}</td>
    </tr>
</c:forEach>
</table>
Sign up to request clarification or add additional context in comments.

3 Comments

I think it should be items with an s.
When I run the code <td>${obj.getItemID()}</td> its telling me that "The function getItemID must be used with a prefix when a default namespace is not specified". What can I do?\
foo and bar are just fields on the target object. I used the standard names not knowing what your object looks like.
0

JSTL is better,

Javascript you should avoid as much as possible ,

I am not sure how you are going to render datatable using java script and Collection

How to use jstl with collection that has been demonstrated by Bozho in the same thread.

7 Comments

Why should avoid JS as much as possible? If there's already a dependency on JS, JS can give you a data grid with many more features than jstl (html alone). All you'd need is to pass the List<Object> as JSON. You could use something like Ext-JS grid for sortable, filterable, reordable hideable columns... dev.sencha.com/deploy/dev/examples/grid/grid-plugins.html
@Juan Mendes If its possible to do with JSTL avoid js because javascript will load browser also not as much reliable. and the case OP is talking about is best suited using JSTL
life.java Your argument doesn't make much sense to me. If your site must work with JS disabled, that may be true, otherwise, your comment sounds like someone who doesn't know much JS. I, for example, require JS since all the interaction is AJAX based. Even if JS is not required, The grid I suggested also supports progressive enhancement. You could output the table with jstl and enhance it to be sortable, filterable, if the client supports JS.
Can we do this thing if the data is to be parsed in diff rows (in 1 loop) as opposed to the normal column flow parsing..
@testndt can you elaborate it a bit
|
0

Javascript doesn't have access to the Java objects that live (I presume) on the server. The server code can make the ArrayLists available to the JSP which can then loop over them with a JSTL forEach tag.

How you make the ArrayLists "available" depends on the framework you're using, but the plain servlet way is just setting an attribute from the doPost method.

request.setAttribute("list1", arrayList1);

The loop would be something like

<table>
<tr><th>Column 1</th> <th>Column 2</th> <th>Column 3</th></tr>
<c:forEach var="row" items="${list1}">
    <tr><td>${row.col1data}</td> <td>${row.col2data}</td> <td>${row.col3data}</td></tr>
</c:forEach>
</table>

Comments

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.