简体   繁体   English

如何在 JSP 文件中以表格格式显示列表内容?

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

In an Action.java file, am using the following piece of code.在 Action.java 文件中,我使用以下代码段。

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

tareWeightList & barcodeList actually holds few values. tareWeightList 和barcodeList 实际上包含很少的值。 After setting the list values to the attributes, the java file forwards the contents to a JSP file.将列表值设置为属性后,java 文件将内容转发到 JSP 文件。

There in JSP file, I can get the contents using below lines,在 JSP 文件中,我可以使用以下几行获取内容,

<%=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.第一列中的条形码值和第二列中对应的 Tareweight 值。

Suggest me an idea for writing the code in JSP file so as the contents are displayed in a tabulated format.建议我在 JSP 文件中编写代码的想法,以便以表格格式显示内容。

Use HTML <table> element to represent a table in HTML.使用 HTML <table>元素表示 HTML 中的表格。 Use JSTL <c:forEach> to iterate over a list in JSP.使用 JSTL <c:forEach>迭代 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've only a design flaw in your code.您的代码中只有一个设计缺陷。 You've split related data over 2 independent lists.您已将相关数据拆分到 2 个独立列表中。 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.我建议创建一个自定义类来将相关数据保存在一起。 Eg例如

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:这样你就可以得到一个List<Product> ,它可以表示如下:

<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:也可以看看:

Use <c:forEach> and <TABLE>使用<c:forEach><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>

我在.jsp页面添加了这个,问题解决了:

<%@ page isELIgnored="false" %>

First of all, you should not use scriptlets in JSP.首先,您不应该在 JSP 中使用 scriptlet。 Use the JSTL, other custom JSP tags, and the expression language.使用 JSTL、其他自定义 JSP 标记和表达式语言。

Second: the point of an MVC framework is to have the action prepare the model for the view, which should stay very simple.第二:MVC 框架的重点是让动作为视图准备模型,这应该非常简单。 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.这样,您的 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 .请注意,存在特定的自定义标签来显示表格、支持排序、分页等。我喜欢使用displaytag

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM