Java 在JSP中显示对象的ArrayList
我试图在jsp页面中显示一个表,其中包含来自Java 在JSP中显示对象的ArrayList,java,jsp,jstl,Java,Jsp,Jstl,我试图在jsp页面中显示一个表,其中包含来自ArrayList的数据 下面是我如何将ArrayList添加到模型中的 List<CustomType> resultList = new ArrayList<CustomType>(); resultList = getResult(); model.addAttribute("resultList", resultList); return "DisplayResult";
ArrayList
的数据
下面是我如何将ArrayList
添加到模型中的
List<CustomType> resultList = new ArrayList<CustomType>();
resultList = getResult();
model.addAttribute("resultList", resultList);
return "DisplayResult";
如何将ArrayList
显示为表
这是我一直在尝试的方法,但似乎不起作用
变量1
变量2
变量3
变量4
变量5
可变的
可变7
变量8
${result.variable1}
${result.variable2}
${result.variable3}
${result.variable4}
${result.variable5}
${result.variable6}
${result.variable7}
${result.variable8}
java文件:
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;
}
publicstaticfruit[]getFruits(){
向量v=新向量();
试一试{
File File=新文件(Controller.path,“Fruit.txt”);
BufferedReader br=新的BufferedReader(新文件读取器(文件));
弦线;
而((line=br.readLine())!=null){
果实f=新果实(系);
v、 添加(f);
}
br.close();
}捕获(例外e){
e、 printStackTrace();
}
水果[]arr=新水果[v.大小();
v、 toArray(arr);
返回arr;
}
jsp文件:
<%
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。您所说的
似乎不起作用是什么意思?预期的和实际的结果是什么?您是否在类中添加了getter方法?因此,当点击表达式${result.variable1}
时,它使用该类中具有相同方法名称的方法,或者getVariable1()
?要么需要添加这些方法,要么需要将它们的格式更改为“普通”java getter方法,以便EL知道要查找什么。
<%
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>
<%}%>