Html 如何在jsp页面动态创建表格的行列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19066966/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to dynamically create row and column of table in jsp page
提问by Java Curious ?
I am trying to retrieve data from database into table. But data must be loaded dynamically.
我正在尝试将数据从数据库检索到表中。但是数据必须动态加载。
How to dynamically create row and column I don't know? If it will be only row to create then I will do it easily but I also want to create column dynamically on page so that's why I am confused how to perform it?
我不知道如何动态创建行和列?如果它只是要创建的行,那么我会很容易做到,但我也想在页面上动态创建列,这就是为什么我对如何执行它感到困惑?
My JSP code :
我的 JSP 代码:
<table width="59%" border="1">
<%
MySql1 o = new MySql1();
o.connect();
ResultSet r;
int counter=1;
String q = "select * from category_master;";
r = o.getdata(q);
while(r.next())
{
%>
<tr>
<td><%= r.getString(1)%></td>
</tr>
<%
}
%>
</table>
Right now I am displaying first column in <td>
but if the user don't know how many columns are going to be retrieved then what to do ?
In select query
I have used *
so I am confused for taking <td>
.
I want to all dynamic because suppose I will pass table name also dynamically using any textbox or url.
现在我正在显示第一列,<td>
但是如果用户不知道要检索多少列,那么该怎么办?在select query
我使用过*
所以我很困惑服用<td>
. 我想要所有动态,因为假设我也将使用任何文本框或 url 动态传递表名。
Here MySql
1 is one class file that has method to perform operation.
connect()
is used to connect with db, and getdata()
is used to retrieve data of query passed as argument and return type of getdata()
method is Resultset
.
这里MySql
1 是一个具有执行操作方法的类文件。
connect()
用于连接db,getdata()
用于检索作为参数传递的查询数据,getdata()
方法的返回类型为Resultset
。
So that's why I want all dynamic, but I don't know how to do that.
所以这就是为什么我想要所有动态,但我不知道该怎么做。
回答by Gaurav Singla
try this code:
试试这个代码:
<table width="59%" border="1">
<%
MySql1 o = new MySql1();
o.connect();
ResultSet r;
int counter=1;
String q = "select * from category_master;";
r = o.getdata(q);
ResultSetMetaData metaData = r.getMetaData();
while(r.next())
{
%>
<tr>
<%
for(int i = 1; i<=metaData.getColumnCount();i++)
{ %>
<td>
<%= r.getString(i)%>
</td>
<%
}
%>
</tr>
<%
}
%>
</table>
回答by Basheer AL-MOMANI
I did jsp page for testing like this
我做了jsp页面进行这样的测试
here it is test.jsp
这里是 test.jsp
<table width="59%" border="1">
<thead>
<tr>
<td>Volume</td>
<td>XmlTitle</td>
<td>getYear</td>
</tr>
</thead>
<tbody>
<%
Journal journal = Journal.findByCode("antipoda");
List<Issue> normalIssues = journal.getIssuesOfType(IssueType.NORMAL);
for (Issue issue : normalIssues) {
out.print(String.format("<tr>" +
"<td>%s</td>" +
"<td>%s</td>" +
"<td>%d</td>" +
"</tr>",
issue.getVolume(),issue.getXmlTitle(),issue.getYear()));
}
out.flush();
%>
</tbody>
</table>
you can change the scriptlet to the code that iterates thru the model you want to display
您可以将 scriptlet 更改为通过要显示的模型进行迭代的代码
hope this helps
希望这可以帮助