5

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.

LGAP
  • 2,365
  • 17
  • 50
  • 71

4 Answers4

16

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:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • @BalusC In my eclipse workshop its showing these tags as unknown tags. Any prob with the JSP version? – LGAP Dec 27 '11 at 15:54
  • You need to declare JSTL taglib in top of your JSP. Click the "Our JSTL wiki page" link for detailed information. I edited the answer to add the declaration example to the 1st snippet. – BalusC Dec 27 '11 at 15:55
  • When I tried using the second piece of code in your answer, ${tareWeight},${barCode} is getting printed in the table and not their corresponding inner values. Please let me know where I have gone wrong. – LGAP Dec 27 '11 at 16:22
  • Fix your `web.xml` to align with the supported servlet API version of your webcontainer which should be at least Servlet 2.4. See also the bottom of the "Our JSTL wiki page" link. – BalusC Dec 27 '11 at 16:23
  • My webcontainer is less than Servlet2.4 it seems. Is there any other way to proceed with this then? – LGAP Dec 27 '11 at 16:52
  • Downgrade JSTL libs to JSTL 1.0 (if the container already don't ship with it, e.g. Tomcat 5.0 or older) and change taglib URIs to remove `/jsp` from the path. Servlet 2.3 is however EOL for long. You're basically working with dead technology. I recommend to upgrade instead. – BalusC Dec 27 '11 at 17:03
4

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>
jmj
  • 237,923
  • 42
  • 401
  • 438
1

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

<%@ page isELIgnored="false" %>
menoktaokan
  • 346
  • 3
  • 13
1

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.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255