LGAP
LGAP

Reputation: 2473

How to Display List Contents in tabular format in a JSP file?

In an Action.java file, am using the following piece of code.

request.setAttribute("TAREWEIGHT", tareWeightList);
    request.setAttribute("BARCODE", barcodeList);
return (mapping.findForward(target));

tareWeightList & barcodeList actually holds few values. After setting the list values to the attributes, the java file forwards the contents to a JSP file.

There in JSP file, I can get the contents using below lines,

<%=request.getAttribute("TAREWEIGHT")%>
<%=request.getAttribute("BARCODE") %>

My requirement is that the contents of that lists should be diplayed in a tabular format.

Barcode values in first column and its corresponding Tareweight values in the second column.

Suggest me an idea for writing the code in JSP file so as the contents are displayed in a tabulated format.

Upvotes: 6

Views: 73476

Answers (4)

menoktaokan
menoktaokan

Reputation: 506

I added this on the .jsp page and the problem has been solved:

<%@ page isELIgnored="false" %>

Upvotes: 1

BalusC
BalusC

Reputation: 1109665

Use HTML <table> element to represent a table in HTML. Use JSTL <c:forEach> to iterate over a list in JSP.

E.g.

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
...
<table>
  <c:forEach items="${list}" var="item">
    <tr>
      <td><c:out value="${item}" /></td>
    </tr>
  </c:forEach>
</table>

You've only a design flaw in your code. You've split related data over 2 independent lists. It would make the final approach as ugly as

<table>
  <c:forEach items="${TAREWEIGHT}" var="tareWeight" varStatus="loop">
    <c:set var="barCode" value="${BARCODE[loop.index]}" />
    <tr>
      <td><c:out value="${tareWeight}" /></td>
      <td><c:out value="${barCode}" /></td>
    </tr>
  </c:forEach>
</table>

I suggest to create a custom class to hold the related data together. E.g.

public class Product {

    private BigDecimal tareWeight;
    private String barCode;

    // Add/autogenerate getters/setters/equals/hashcode and other boilerplate.
}

so that you end up with a List<Product> which can be represented as follows:

<table>
  <c:forEach items="${products}" var="product">
    <tr>
      <td><c:out value="${product.tareWeight}" /></td>
      <td><c:out value="${product.barCode}" /></td>
    </tr>
  </c:forEach>
</table>

after having put it in the request scope as follows:

request.setAttribute("products", products);

See also:

Upvotes: 17

JB Nizet
JB Nizet

Reputation: 692191

First of all, you should not use scriptlets in JSP. Use the JSTL, other custom JSP tags, and the expression language.

Second: the point of an MVC framework is to have the action prepare the model for the view, which should stay very simple. You should probably make the action create a single list of objects, each containing the fields from the barcodes and their corresponding tareweight. This way, you would just have one iteration in your JSP.

The code would thus be very simple:

<c:forEach var="barCodeAndTareWeight" items="BARCODE_AND_TAREWEIGHTS">
    <tr>
        <td><c:out value="${barCodeAndTareWeight}.someField"/></td>
        <td><c:out value="${barCodeAndTareWeight}.someOtherField"/></td>
    </tr>
</c:forEach>

Note that there exist specific custom tags to display tables, support sorting, paging etc. I like using the displaytag.

Upvotes: 1

Jigar Joshi
Jigar Joshi

Reputation: 240976

Use <c:forEach> and <TABLE>

For Example :

<TABLE>
<c:forEach items="${personList} var="peson">

  <tr>
    <td><c:out value="${person.name}"/></td>
    <td><c:out value="${person.age}"/></td>
  </tr>
</c:forEach>
</TABLE>

Upvotes: 4

Related Questions