2012-10-02 63 views
1

我使用MVC在Java中開發Web項目。
我的問題是我需要從一個類中返回三個不同的bean。所有三個bean都有多個對象,所以我現在在列表中添加每個相同的bean對象並返回三個不同的列表。
好吧,讓它更清晰我需要從存儲評論的表中檢索所有內容。因此,所有評論文本都存儲在一個名爲comment的bean中,並添加到名爲listcomment的列表中。發表評論的成員的名稱被添加到另一個名爲member的bean中,並再次添加到名爲listmember的列表中。
那麼是否有任何可能的方式,使這兩個豆可以添加在同一個列表中?在同一個列表中返回兩個不同的bean

public class TeleCommentView { 

int qid; 
TeleComment comment; 
TeleHospital hospital; 
doctorperson doctor; 

ConnectionFile connection = new ConnectionFile(); 
List<TeleComment> listcomment = new ArrayList<TeleComment>(); 
List<doctorperson> listdoctor = new ArrayList<doctorperson>(); 
List<TeleHospital> listhospital = new ArrayList<TeleHospital>(); 




public TeleCommentView(int qid) 
{ 
    this.qid = qid; 
    process(); 
} 
public void process() 
{ 
    int count=0; 
    try 
    { 
    Connection con = connection.connectionfile(); 
    PreparedStatement pstmt = con.prepareStatement("select TeleHospital.HospitalName,DoctorDetail.Name,TeleComment.Comment,TeleComment.SDate from" 
                + "((TeleComment left join TeleHospital on TeleHospital.HospitalId=TeleComment.Hid) " 
                + "left join DoctorDetail on DoctorDetail.DoctorId = TeleComment.Did) " 
                + "where TeleComment.Qid=?"); 
    ResultSet rs = pstmt.executeQuery(); 

    while(rs.next()) 
    { 
     comment = new TeleComment(); 

     comment.setComment(rs.getString("Comment")); 
     comment.setSdate(rs.getDate("SDate")); 

     listcomment.add(count,comment) ; 

     /******End of comment**************/ 

     //Add doctor or hospital name as required 

     doctor = new doctorperson(); 
     hospital = new TeleHospital(); 

     if(rs.getString("HospitalName").equals(null)) 
      { 
       doctor.setName(rs.getString("Name")); 
       listdoctor.add(count,doctor); 
      } 
      else 
       { 
        hospital.setHospitalname(rs.getString("HospitalName")); 
        listhospital.add(count,hospital); 
       } 
     count++; 
     } 
    } 
    catch(Exception ex) 
    { 
     ex.printStackTrace(); 
    } 

} 

    public List getCommentList() 
    { 
    return listcomment; 
    } 

public List getDoctorList() 
    { 
    return listdoctor; 
    } 

public List getHospitalList() 
    { 
    return listhospital; 
    } 

} 
+0

首先編寫代碼。 –

回答

2

如果不同的豆類都含有一定的方法(或方法),你可以創建一個interface,讓每個bean實現它。

這裏有一個類似的問題,即剛剛問:

Treating different objects the same

傻示例代碼:

interface CommentItem { 
    public String getComment(); 
} 


class ModeratorComment implements CommentItem { 
    public String getComment() { 
     return "Comment from moderator"; 
    } 
    // other moderator-specific code... 
} 



class StudentComment implements CommentItem { 
    public String getComment() { 
     return "Comment from student"; 
    } 
    // other student-specific code... 
} 


class CommentContainer { 

    private List<CommentItem> commentList; 

    public List<CommentItem> getCommentList() { 
     return commentList; 
    } 

    public void addComment(CommentItem someComment) { 
     commentList.add(someComment); 
    } 
} 


class TestIt() { 

    public static void main(String[] args) { 

     StudentComment sc = new StudentComment(); 
     ModeratorComment mc = new ModeratorComment(); 

     CommentContainer comments = CommentContainerFactory.createCommentContainer(); 
     comments.add(sc); 
     comments.add(mc); 

     for (CommentItem ci : comments.getCommentList()) { 
      System.out.println(ci.getComment()); 
     } 

    } 

} 
相關問題