使用Java jsp在表视图中显示数据

时间:2015-09-15 02:46:49

标签: java html mysql jsp

我是java jsp的新手。我尝试将数据显示到表视图中。但似乎有一些问题,如果我改变我的rowpan和显示另一个人的td。

<tr><td rowspan="3"><%=owner.getOname()%></td> </tr> 

有没有人能够提供一些指导?这是我的代码。

<%@page import="com.entity.Owner"%>
<%@page import="java.util.List"%>
<%@page import="java.math.BigDecimal"%>
<%@page import="java.util.Date"%>
<%@page import="com.entity.Pen"%>
<%@page import="com.session.PenFacadeLocal"%>
<%@page import="javax.naming.InitialContext"%>
<%
    try{
        //initialisation
        InitialContext ctx = new InitialContext();
        PenFacadeLocal penFacade = (PenFacadeLocal)ctx.lookup("java:comp/env/pen");

        List<Owner> ownerList = penFacade.listByOwner(); %>

        <table border="1" align="center" style="width:50%">
        <thead>
            <tr>
                <th>Owner Name</th>
                <th>Brand of Pen</th>
                <th>Colour</th>
            </tr>
        </thead>
        <tbody>

        <% for (Owner owner : ownerList) { %>
        <tr><td rowspan="3"><%=owner.getOname()%></td> </tr>

            <% for (Pen pen : owner.getPenSet()) { %>
            <tr>
                <td><%=pen.getPbrand()%></td>
                <td><%=pen.getPcolour()%></td>
            </tr>
            <% }
            out.print("</br>");
        }

    } catch(Exception e) {
        e.printStackTrace();
        out.print("OK : " + e.getMessage());
    }
%>

        </tbody>
        </table>

我想得到的表格视图是 table view that i should get

这是我得到的表格输出 table view

1 个答案:

答案 0 :(得分:0)

请尝试以下代码

<% for (Owner owner : ownerList) { 
    // assuming getPenSet() returns set object.
    Set<Pen> pens = owner.getPenSet();
%>
    <!-- Here I am adding rowspan value dynamically, depends no of pens.
     Adding `+1` to no of pens because the Owner is also counted for the rowspan. -->
    <tr><td rowspan="<%=pens.size()+1>"><%=owner.getOname()%></td> </tr>

        <% for (Pen pen : pens) { %>
        <tr>
            <td><%=pen.getPbrand()%></td>
            <td><%=pen.getPcolour()%></td>
        </tr>
        <% }
       // no need of <br> inside of `<table>`
       // out.print("</br>");
    }