如何在JSP中显示列表? [英] How to show a list in JSP?
问题描述
这个Servlet,我在彩色车上做GROUP BY.插入请求并在jsp中做广告但不会转换.
查询查询= session.createQuery(从cars组中按carColor按carColor排序,从cars组中选择count(carColor),carColor");List<汽车>列表= query.list();迭代器iter = list.iterator();while(iter.hasNext()){Object [] obj =(Object [])iter.next();System.out.println(obj [0] +" + obj [1]);}request.setAttribute("list",list);RequestDispatcher rd = request.getRequestDispatcher("test.jsp");rd.forward(请求,响应);
控制台:2白10黑色5蓝色
JSP:[[Ljava.lang.Object; @ 1f3b536,[Ljava.lang.Object; @ fdffb1,]]
您的代码确实没有任何意义:
List< Cars>列表= query.list();
在上一行中,您声明您的列表是Cars列表(不是).
Iterator iter = list.iterator();while(iter.hasNext()){Object [] obj =(Object [])iter.next();
然后在列表上进行迭代,并将每个元素转换为 Object []
.Cars实例如何成为 Object []
?该列表应声明为 List< Object []>
,并且您不应使用原始类型.您的循环应写为
Iterator< Object []>iter = list.iterator();while(iter.hasNext()){Object [] obj = iter.next();
或者,甚至更简单:
for(Object [] obj:list){
现在,在JSP中,我怀疑您只是在使用 $ {list}
来显示列表.这只是在列表上调用toString()方法,而列表本身会调用每个元素的toString()方法.由于每个元素都是一个 Object []
,因此结果字符串为 [Ljava.lang.Object; @ 1f3b536
",表示具有hashCode 1f3b536的对象数组".>
要显示列表的元素,应遍历该列表,就像必须在Java代码中进行的操作一样:
< c:forEach var ="array" items ="$ {list}">计数:$ {array [0]}-颜色:< c:out value ="$ {array [1]}"//>< br/></c:forEach>
This Servlet, I do GROUP BY on color car. Inserted into the request and advertise in jsp but it does not convert.
Query query = session.createQuery("select count(carColor), carColor from Cars group by carColor order by carColor");
List<Cars> list = query.list();
Iterator iter = list.iterator();
while (iter.hasNext()) {
Object[] obj = (Object[]) iter.next();
System.out.println(obj[0] + " " + obj[1]);
}
request.setAttribute("list", list);
RequestDispatcher rd = request.getRequestDispatcher("test.jsp");
rd.forward(request, response);
Console: 2 White 10 Black 5 Blue
JSP: [[Ljava.lang.Object;@1f3b536, [Ljava.lang.Object;@fdffb1,]]
Your code really makes no sense:
List<Cars> list = query.list();
In the above line, you declare that your list is a list of Cars (which it is not)
Iterator iter = list.iterator();
while (iter.hasNext()) {
Object[] obj = (Object[]) iter.next();
Then you iterate on the list, and cast each element to Object[]
. How could a Cars instance ever be an Object[]
? The list should be declared as List<Object[]>
, and you shouldn't use raw types. Your loop should be written as
Iterator<Object[]> iter = list.iterator();
while (iter.hasNext()) {
Object[] obj = iter.next();
Or, even simpler:
for (Object[] obj : list) {
Now, in the JSP, I suspect you're just using ${list}
to display the list. This simply calls the toString() method on the list, which itself calls the toString() method of each element. Since each element is an Object[]
, the result string is [Ljava.lang.Object;@1f3b536
, which means "array of Object with hashCode 1f3b536".
To display the elements of the list, you should iterate over the list, as you must do it in the Java code:
<c:forEach var="array" items="${list}">
Count: ${array[0]} - Color: <c:out value="${array[1]}"/> <br/>
</c:forEach>
这篇关于如何在JSP中显示列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!