How to display the contents of a list in tabular format in a JSP file?

In the Action.java file, I use the following code snippet.

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

tareWeightList and barcodeList actually contain multiple values. After setting the list values โ€‹โ€‹to java attributes, the file sends the contents to the JSP file.

In the JSP file, I can get the contents using the lines below,

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

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

The barcode values โ€‹โ€‹in the first column and the corresponding Tareweight values โ€‹โ€‹in the second column.

Offer me the idea of โ€‹โ€‹writing code in a JSP file so that the content is displayed in tabular format.

+4
source share
3 answers

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

eg.

 <%@ 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 only have a design flaw in your code. You split the related data over two independent lists. This will 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 creating your own class for storing related data. For instance.

 public class Product { private BigDecimal tareWeight; private String barCode; // Add/autogenerate getters/setters/equals/hashcode and other boilerplate. } 

so that you get 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 placing it in the request area as follows:

 request.setAttribute("products", products); 

See also:

+9
source

Use <c:forEach> and <TABLE>

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> 
+2
source

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

Secondly: the point of the MVC framework is to force the action to prepare the model for presentation, which should remain very simple. You should probably take an action by creating a single list of objects, each of which contains barcode fields and their corresponding weight. This way you will only have one iteration in your JSP.

Thus, the code will 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> 

Please note that there are specific user tags for displaying tables, sorting support, swapping, etc. I like to use displaytag .

+1
source

Source: https://habr.com/ru/post/1388155/


All Articles