如何在JSP文件中以表格格式显示列表内容? [英] How to Display List Contents in tabular format in a JSP file?

查看:896
本文介绍了如何在JSP文件中以表格格式显示列表内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Action.java文件中,我正在使用以下代码。

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实际上只包含很少的值。
将列表值设置为属性后,java文件将内容转发到JSP文件。

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

在JSP文件中,我可以使用以下行,

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.

第一列中的条形码值及其在第二列中的相应Tareweight值。

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

建议我编写代码的想法JSP文件,以便以列表格式显示内容。

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

推荐答案

使用HTML < table> 元素表示HTML中的表。使用JSTL < c:forEach> 迭代JSP中的列表。

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

例如

<%@ 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>

您的代码中只存在设计缺陷。您已将相关数据拆分为2个独立列表。它会使最终的方法变得丑陋,因为

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.
}

这样你最终得到的是 List< Product> ; 可以表示如下:

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);



参见:



查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆