英文:
Display ArrayList of Objects in JSP
问题
我尝试在 JSP 页面中显示来自 ArrayList
的数据表格。
以下是我如何将 ArrayList
添加到模型中:
List<CustomType> resultList = new ArrayList<CustomType>();
resultList = getResult();
model.addAttribute("resultList", resultList);
return "DisplayResult";
这是 CustomType
类的定义:
public class CustomType implements SQLDataType {
private String variable1;
private String variable2;
private String variable3;
private String variable4;
private String variable5;
private String variable6;
private String variable7;
private Timestamp variable8;
// 其他代码...
}
我该如何将这个 ArrayList
显示为表格?
以下是我尝试过但似乎不起作用的方法:
<div id="resultTable">
<table>
<tr>
<th>variable1</th>
<th>variable2</th>
<th>variable3</th>
<th>variable4</th>
<th>variable5</th>
<th>variable6</th>
<th>variable7</th>
<th>variable8</th>
</tr>
<c:forEach var="result" items="${resultList}">
<tr>
<td>${result.variable1}</td>
<td>${result.variable2}</td>
<td>${result.variable3}</td>
<td>${result.variable4}</td>
<td>${result.variable5}</td>
<td>${result.variable6}</td>
<td>${result.variable7}</td>
<td>${result.variable8}</td>
</tr>
</c:forEach>
</table>
</div>
英文:
I was trying to display a table in jsp page with data from ArrayList
.
Here is how I added the ArrayList
to Model
List<CustomType> resultList = new ArrayList<CustomType>();
resultList = getResult();
model.addAttribute("resultList", resultList);
return "DisplayResult";
Here is the CustomType
:
public class CustomType implements SQLDataType {
private String variable1;
private String variable2;
private String variable3;
private String variable4;
private String variable5;
private String variable6;
private String variable7;
private Timestamp variable8;
....
..
How can I display the ArrayList
as Table ?
Here's how I've been trying this and it doesn't seem to work
<div id="resultTable">
<table>
<tr>
<th>variable1</th>
<th>variable2</th>
<th>variable3</th>
<th>variable4</th>
<th>variable5</th>
<th>variable6</th>
<th>variable7</th>
<th>variable8</th>
</tr>
<c:forEach var="result" items="${resultList}">
<tr>
<td>${result.variable1}</td>
<td>${result.variable2}</td>
<td>${result.variable3}</td>
<td>${result.variable4}</td>
<td>${result.variable5}</td>
<td>${result.variable6}</td>
<td>${result.variable7}</td>
<td>${result.variable8}</td>
</tr>
</c:forEach>
</table>
</div>
答案1
得分: -1
public static Fruit[] getFruits(){
Vector<Fruit> v = new Vector();
try{
File file = new File(Controller.path, "Fruit.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while((line = br.readLine()) != null){
Fruit f = new Fruit(line);
v.add(f);
}
br.close();
}catch(Exception e){
e.printStackTrace();
}
Fruit[] arr = new Fruit[v.size()];
v.toArray(arr);
return arr;
}
<%
Fruit[] arr = (Fruit[]) request.getAttribute("labelList");
if(arr != null){
%>
<table border='1'>
<% for(Fruit fr : arr){ %>
<tr>
<td>
<%=fr.id%>
</td>
<td>
<a href='C?cmd=Details&id=<%=fr.id%>'><%=fr.nameFruit%></a>
</td>
</tr>
<%}%>
</table>
<%}%>
首先,从数据库加载数据并将其存储在列表中,然后将其发送到 JSP 页面。
英文:
java file:
public static Fruit[] getFruits(){
Vector<Fruit> v = new Vector();
try{
File file = new File(Controller.path,"Fruit.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while((line = br.readLine()) != null){
Fruit f = new Fruit(line);
v.add(f);
}
br.close();
}catch(Exception e){
e.printStackTrace();
}
Fruit[] arr = new Fruit[v.size()];
v.toArray(arr);
return arr;
}
jsp file:
<%
Fruit[] arr = (Fruit[]) request.getAttribute("labelList");
if(arr != null){
%>
<table border='1'>
<% for(Fruit fr : arr){ %>
<tr>
<td>
<%=fr.id%>
</td>
<td>
<a href='C?cmd=Details&id=<%=fr.id%>'><%=fr.nameFruit%></a>
</td>
</tr>
<%}%>
</table>
<%}%>
First of all, Load the data from the database and store it in the list, then send it to jsp.
专注分享java语言的经验与见解,让所有开发者获益!
评论