日期:2014-05-19  浏览次数:20697 次

帮忙看看下面这短代码,小弟已经晕了!
Dao层如下:
public List<Object[]> get_collect_message(String plantype) {
String hql="select isi,sum(isi.tounumber) as tounumber,max(isi.rukutime) as rukutime  
  from InStoreInfo as isi where isi.plantype=? and isi.isAgree !=0 and isi.inuse=0 
  group by isi.plancode order by rukutime desc";
Query query=sf.getCurrentSession().createQuery(hql);
query.setParameter(0, plantype);
return query.list();
}
Service如下:

public List<RuLanCollectMessage> get_collect_message(String plantype) {
List<Object[]> list=storeDao.get_collect_message(plantype);
System.out.println(list.size());//取得list.size()=2
List RLCMList=new ArrayList();
RuLanCollectMessage ruLanCollectMessage=new RuLanCollectMessage();//自定义的一个类
for(int i=0;i<list.size();i++){
Object[] obj=list.get(i);//得到索引为i的那条记录
InStoreInfo isi=(InStoreInfo) obj[0];//obj[0]是一个InStoreInfo对象
ruLanCollectMessage.setPlancode(isi.getPlancode());
  System.out.println(isi.getPlancode());//输出 20120728,022
ruLanCollectMessage.setGuocode(isi.getGuocode());
ruLanCollectMessage.setLasttime((Date)obj[2]);
Long l=(Long)obj[1];
ruLanCollectMessage.setInputAllDateTou(l.intValue());
RLCMList.add(ruLanCollectMessage);
for(int y=0;y<RLCMList.size();y++){
RuLanCollectMessage r=(RuLanCollectMessage)RLCMList.get(i);
System.out.println(r.getPlancode());

}
}
return RLCMList;
}
在下面的循环语句处我输出System.out.println(r.getPlancode());
控制台输出 20120728,022,022。jsp页面显示是两条记录 022,022
然而正如上面所标注 第一个list.size()=2的 说明只有2条记录。为什么输输出3次?就算3次吧 那为什么jsp页面遍历却又显示后面两个022,022呢?

我将service做如下修改:
  public List<RuLanCollectMessage> get_collect_message(String plantype) {
List<Object[]> list=storeDao.get_collect_message(plantype);
System.out.println(list.size());
List RLCMList=new ArrayList();
for(int i=0;i<list.size();i++){
RuLanCollectMessage ruLanCollectMessage=new RuLanCollectMessage();
Object[] obj=list.get(i);
InStoreInfo isi=(InStoreInfo) obj[0];
ruLanCollectMessage.setPlancode(isi.getPlancode());
ruLanCollectMessage.setGuocode(isi.getGuocode());
ruLanCollectMessage.setLasttime((Date)obj[2]);
Long l=(Long)obj[1];
ruLanCollectMessage.setInputAllDateTou(l.intValue());
RLCMList.add(ruLanCollectMessage);
for(int y=0;y<RLCMList.size();y++){
RuLanCollectMessage r=(RuLanCollectMessage)RLCMList.get(i);
System.out.println(r.getPlancode());

}
}
return RLCMList;
}
我把RuLanCollectMessage ruLanCollectMessage=new RuLanCollectMessage();放到for循环里面去
现在System.out.println(r.getPlancode());输出还是:
20120728,022,022 三个 但是页面却对了 显示的是20120728,022。

说明:list.size()=2是对的就是两条记录 一个plancode是20120728;另一个是022.
我的几个疑问:
  1.为什么两种写法都是输出 :20120728,022,022 按照正常情况下 应该只是20120728,022 为什么多出一个022
  2.既然2中写法都输出一样的结果 为什么页面显示却又不一样呢?
真心求解释,那里错了?我已经晕了.............
为了更全面 我贴出Action和jsp
Action:
  public String show_collect_message(){
try{
String plantype="2";
List<RuLanCollectMessage> list = storeService.get_collect_message(plantype);
ServletActionContext.getRequest().setAttribute("list", list);
}catch (Exception e) {
e.printStackTrace();
}
return "show_collect_message";
}
jsp:
  <c:forEach items="${requestScope.list}" var="obj">
<tr>
<td>${obj.plancode }</td>
</tr>
</c:forEach>