打印表循环错误

时间:2015-03-13 12:12:27

标签: java jsp for-loop printing html-table

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

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

2 个答案:

答案 0 :(得分:3)

把这些行:

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

答案 1 :(得分:1)

我也试过这个:

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

稍微好一点的imho。但是如果没有你的帮助,我不可能做到这一点。谢谢。