如何通过单击超链接从Java中的arraylist中检索值

时间:2013-10-21 17:53:58

标签: java jsp servlets

我正在开发一个Java应用程序eInvoice应用程序。应用程序的目的是添加电子发票,然后显示我添加的电子发票(这里完全正常工作)。但是,当我显示发票时,我希望公司名称被超链接,并且在点击链接后,应该在Jsp页面中显示特定发票详细信息。请注意我将信息传递给servlet,然后使用getter和Setter将信息存储在java类中。多个记录存储在Arraylist对象中。我似乎无法找到这样做的方法。任何帮助是极大的赞赏。

<%
float bigTotal=0;
try
{
    @SuppressWarnings("unchecked")
    ArrayList<UserInvoice> myVal = (ArrayList<UserInvoice>)session.getAttribute("eInvoice");
    out.write("<table border='0'>");
    out.write("<th width='200'>Invoice No.</th><th width='300'>Client Name</th>  <th width='200'>Total</th><th width='200'>Payment Due Date</th>");
    for(UserInvoice invoices:myVal)
    {
        out.write("<tr>");
        out.write("<td height='25'>");
        out.write(""+invoices.getInvoiceNo());
        out.write("</td>");

        out.write("<td>");
        out.write(invoices.getClientName());
        out.write("</td>");

        out.write("<td>");
        out.write(String.valueOf(invoices.getTotal()));
        out.write("</td>");

        out.write("<td>");
        out.write(invoices.getPayDueDate());
        out.write("</td>");

        out.write("</tr>");
        bigTotal = bigTotal+invoices.getTotal();

    }
    out.write("</table>");
}
catch(Exception ex)
{
out.write("<h1>Looks like you haven't added any invoices yet.</h1>");
}

1 个答案:

答案 0 :(得分:1)

您可以使用JSTL foreach

<%@ page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<% request.setAttribute("eInvoices", eInvoices); %>

<table>
 <c:forEach items="${eInvoices}" var="value">
  <tr><td><c:out value="${value.name}"/></td></tr>
 </c:forEach>
</table>

使用AJAX获取详细信息。

(当您点击发票名称时,会向页面发出HTTP请求以获取详细信息。这些详细信息将以HTML格式返回,内容类型为'text/html'

示例: