在Action.java文件中,我使用以下代码.
request.setAttribute("TAREWEIGHT", tareWeightList);
request.setAttribute("BARCODE", barcodeList);
return (mapping.findForward(target));
Run Code Online (Sandbox Code Playgroud)
tareWeightList和barcodeList实际上只包含很少的值.将列表值设置为属性后,java文件将内容转发到JSP文件.
在JSP文件中,我可以使用下面的行获取内容,
<%=request.getAttribute("TAREWEIGHT")%>
<%=request.getAttribute("BARCODE") %>
Run Code Online (Sandbox Code Playgroud)
我的要求是该列表的内容应以表格格式显示.
第一列中的条形码值及其在第二列中的相应Tareweight值.
建议我在JSP文件中编写代码,以便以列表格式显示内容.
Bal*_*usC 15
使用HTML <table>元素表示HTML中的表.使用JSTL <c:forEach>迭代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>
Run Code Online (Sandbox Code Playgroud)
你的代码中只有一个设计缺陷.您已将相关数据拆分为2个独立列表.它会让最后的方法变得丑陋
<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>
Run Code Online (Sandbox Code Playgroud)
我建议创建一个自定义类来保存相关数据.例如
public class Product {
private BigDecimal tareWeight;
private String barCode;
// Add/autogenerate getters/setters/equals/hashcode and other boilerplate.
}
Run Code Online (Sandbox Code Playgroud)
所以你最终得到一个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>
Run Code Online (Sandbox Code Playgroud)
将它放入请求范围后如下:
request.setAttribute("products", products);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
58692 次 |
| 最近记录: |