我用hibernate做了一个数据库查询,返回的结果是List.我现在想通过<logic:iterator>来循环输出其中的内容,请问我该怎么做。我看到书上写<logic:iterator>可输出的是集合或map,我直接把list作为变量,输出的全不是我要的东西。我是这么写的:
hibernate 映射数据库的类:
package com.rey.hibernate.po;
import java.util.*;
public class OrmUser
{
    private Integer UserId;
    private String UserName;
    private String Password;
    private String EMail;
    private short RType;
    private Set Sorts;////////////Connected with OrmSort
    private Set Articles;/////////Connected with OrmArticle
    
    public void setUserId(Integer inUserId)
    {
        this.UserId=inUserId;
    }
    public void setUserName(String inUserName)
    {
        this.UserName=inUserName;
    }
    public void setPassword(String inPassword)
    {
        this.Password=inPassword;
    }
    public void setEMail(String inEMail)
    {
        this.EMail=inEMail;
    }
    public void setRType(short inRType)
    {
        this.RType=inRType;
    }
    public void setSorts(Set inSorts)
    {
        this.Sorts=inSorts;
    }
    public void setArticles(Set inArticles)
    {
        this.Articles=inArticles;
    }
    
    public Integer getUserId()
    {
        return this.UserId;
    }
    public String getUserName()
    {
        return this.UserName;
    }
    public String getPassword()
    {
        return this.Password;
    }
    public String getEMail()
    {
        return this.EMail;
    }
    public short getRType()
    {
        return this.RType;
    }
    public Set getSorts()
    {
        return this.Sorts;
    }
    public Set getArticles(){
        return this.Articles;
    }
进行hibernate数据库查询的:
package com.rey.hibernate.operation;
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Query;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;import com.rey.hibernate.po.*;
import com.rey.hibernate.HibernateStart;
import java.util.*;
/**
 * @author Administrator
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class Userproc
{
        public static List Getuserlist(int PageIndex,int TotalPerPage) throws HibernateException
    {
        Session s=null;
    Transaction x=null;
int FirstResult=(PageIndex-1)*TotalPerPage;
List list=null;
try
{
    s=HibernateStart.openSession();        
x=s.beginTransaction();
    Query q=s.createQuery(" from OrmUser ou  order by ou.UserId desc");
    q.setFirstResult(FirstResult);
    q.setMaxResults(TotalPerPage);
    list=q.list();
    x.commit();
}
catch(HibernateException e)
{
    e.printStackTrace();
    if(x != null) {
                x.rollback();
            }throw e;
}
finally 
{
            s.close();
        }
return list;
        
    }
}这样,我的jsp页面是:
<%@ page  contentType="text/html; charset=gb2312" 
language="java" import="java.sql.*" errorPage="" %>
<%@ page import="java.util.*"%>
<%@ page import="com.rey.hibernate.operation.*"%>

<%@ taglib uri="/struts-bean" prefix="bean" %>
<%@ taglib uri="/struts-html" prefix="html" %>
<%@ taglib uri="/struts-logic" prefix="logic" %>

<!DOCTYPE HTML PUBLIC "-//w3c//dtd html 4.0 transitional//en">
<html:html locale="true">
<head>
<title>新的用户</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body bgcolor="#FFFFFF">
<html:errors property="haha"/>
<br>
<%
List li=Userproc.Getuserlist( 0,10);
request.setAttribute( "useli",li);
%>
<logic:iterate id="user" name="useli">
<bean:write name="user" />
<br>
</logic:iterate>
</body>
</html:html>
这样输出的都是什么:
com.rey.hibernate.po.OrmUser@1bb205a 
com.rey.hibernate.po.OrmUser@48fbc0 
我想输出user对应的属性,该怎么办?

解决方案 »

  1.   

    试试:
    <bean:write name="user.UserName" />
      

  2.   

    出错了:
    javax.servlet.ServletException: Cannot find bean user.UserName in any scope
    是不是要进行下类型转换,弄成OrmUser的?
    可是要怎么弄?
      

  3.   


     <logic:present name="useli">  //最好用这个,先判断useli存在不存在
    <logic:iterate id="user" name="useli"> <bean:write name="user" property="这时写字段名字" />
    比如
            <bean:write name="user" property="id" />         
    <br>
    </logic:iterate>
     </logic:present >
      

  4.   

    plusjava,还是出错,说找不到属性的方法啊:
    javax.servlet.ServletException: No getter method for property UserName of bean user
    可我明明上面写的
      

  5.   

    你是想输出username吗?
    如果这样,
    改成<logic:iterate id="UserName" name="useli">
    <bean:write name="UserName" />
    <br>
    </logic:iterate>我建议你最好将这个pojo类的成员都小写!
      

  6.   

    不对的,他说找不到属性username的getter方法。可是上面明明有的。
      

  7.   

    在逻辑标记库中定义了<logic:iterate>标记,它能够根据特定集合中元素的数目对标记体的内容进行重复的检查。集合的类型可以是java.util.Iterator,java.util.Collection,java.util.Map或是一个数组。
    重复标记<logic:iterate>的属性定义如下:
    collection 如果没有设置name属性,它就指定了要进行重复的集合
    Id   页作用域bean和脚本变量的名称,它保存着集合中当前元素的句柄
    indexed   页作用域JSP bean的名称,它包含着每次重复完成后集合的当前索引
    Length   重复的最大次数
    Name   作为集合的bean的名称,或是一个bean名称,它由property属性定义的属性,是个集合
    Offset   重复开始位置的索引
    property   作为集合的Bean属性的名称
    Scope   如果指定了bean名称,这个属性设置bean的作用域。若没有设置,搜索范围从页到应用程序作用域
    Type   为当前定义的页作用域bean的类型