打印 table for 循环错误

Print table for loop error

大家好,我有这个 jsp 页面,在调用方法 showAvailableCards() 后应该打印所有可用的卡片。这发生在循环遍历 ArrayList 的 for 循环中,其余的由 out.print 完成。问题是我的 for 循环为每条记录打印一个 table。这个怎么解决在我的代码下面:

                    List<Card> cards = new ArrayList<Card>();
                    Association asso = new Association();
                    cards = asso.showAvailableCards();
                    out.print("<br>");
                    out.print("<h2>Available cards:</h2>");

                    for (int i = 0; i < cards.size(); i++) {
                        Card card = new Card();
                        card = cards.get(i);


                    out.print("<Table width=110% align=center border='1'>");
                    out.print("<td><input type=hidden name=\"idcard\" ></td>");
                    int idcard = card.getId();
                    out.print("<tr><th>card Id</th>\n<th>Card number</th>\n <th>Attivation code</th>\n <th>Pin</th>\n <th>Amount</th>\n <th>Expiry date</th>\n <th>Select</th>\n</tr>");
                    out.print("<td align=center>" + card.getId()+ "</td>");
                    out.print("<td align=center>" + card.getCardNumber()+ "</td>");
                    out.print("<td align=center>" + card.getAttCode()+ "</td>");
                    out.print("<td align=center>" + card.getPin() + "</td>");
                    out.print("<td align=center>&#8364; "+ card.getAmount() + "</td>");
                    out.print("<td align=center>" + card.getExpiryMonth()+ "/" + card.getExpiryYear() + "</td>");
                    out.print("<td align= center><input type=\"button\" value=\"Select\" onclick=\"exeForm(1,'CardExe.jsp',"+card.getId()+")\">");

                    out.print("</table>");
                    }

输入这些行:

out.print("<Table width=110% align=center border='1'>");

在循环之前。 循环后的这一行:

out.print("</table>");

此外:在循环中添加 tr 标签。

所以你得到:

       out.print("<Table width=110% align=center border='1'>");
 for (int i = 0; i < cards.size(); i++) {
                    Card card = new Card();
                    card = cards.get(i);


               out.print("<tr>");
                out.print("<td><input type=hidden name=\"idcard\" ></td>");
                int idcard = card.getId();
                out.print("<tr><th>card Id</th>\n<th>Card number</th>\n <th>Attivation code</th>\n <th>Pin</th>\n <th>Amount</th>\n <th>Expiry date</th>\n <th>Select</th>\n</tr>");
                out.print("<td align=center>" + card.getId()+ "</td>");
                out.print("<td align=center>" + card.getCardNumber()+ "</td>");
                out.print("<td align=center>" + card.getAttCode()+ "</td>");
                out.print("<td align=center>" + card.getPin() + "</td>");
                out.print("<td align=center>&#8364; "+ card.getAmount() + "</td>");
                out.print("<td align=center>" + card.getExpiryMonth()+ "/" + card.getExpiryYear() + "</td>");
                out.print("<td align= center><input type=\"button\" value=\"Select\" onclick=\"exeForm(1,'CardExe.jsp',"+card.getId()+")\">");
               out.print("</tr>");

                }
                out.print("</table>");

我也试过这个:

                List<Card> cards = new ArrayList<Card>();
                Association asso = new Association();
                cards = asso.showAvailableCards();
                out.print("<br>");
                out.print("<h2>Available cards:</h2>");
                out.print("<Table width=100% align=center border='1'>");
                out.print("<input type=hidden name=\"idcard\">");
                out.print("<tr><th>card Id</th><th>Card number</th><th>Attivation code</th> <th>Pin</th><th>Amount</th><th>Expiry date</th> <th>Select</th></tr>");

                for (int i = 0; i < cards.size(); i++) {
                    Card card = new Card();
                    card = cards.get(i);

                    out.print("<tr>");
                    int idcard = card.getId();
                    out.print("<td align=center>" + card.getId() + "</td>");
                    out.print("<td align=center>" + card.getCardNumber + "</td>");
                    out.print("<td align=center>" + getAttCode() + "</td>");
                    out.print("<td align=center>" + card.getPin() + "</td>");
                    out.print("<td align=center>&#8364; " + card.getAmount()
                            + "</td>");
                    out.print("<td align=center>" + card.getExpiryMonth() + "/"
                            + card.getExpiryYear() + "</td>");
                    out.print("<td align= center><input type=\"button\" value=\"Select\" onclick=\"exeForm(1,'CardExe.jsp',"
                            + card.getId() + ")\">");
                    out.print("</tr>");

                }
                out.print("</table>");

恕我直言,稍微好一点。然而,如果没有你的帮助,我不会那样做。谢谢。