2014-10-04 81 views
0

看來我不懂繼承。 我有這些類:PicaAsset,VideoAsset從類名繼承資產。 這是資產類聲明:繼承+ RPC GWT

public class Assets { 
protected int book=0; 
protected int fromChapter=0; 
protected int toChapter=0; 
protected int fromVerse=0; 
protected int toVerse=0; 
protected String creator=null; 
protected String discription=null; 
protected String source=null; 
protected String title=null; 
protected String duration=null; 
protected String url=null; 

public Assets(int book, int fromChapter, int toChapter, int fromVerse, 
     int toVerse, String creator, String discription, String source, 
     String title, String duration, String url) { 

    this.book = book; 
    this.fromChapter = fromChapter; 
    this.toChapter = toChapter; 
    this.fromVerse = fromVerse; 
    this.toVerse = toVerse; 
    this.creator = creator; 
    this.discription = discription; 
    this.source = source; 
    this.title = title; 
    this.duration = duration; 
    this.url = url; 
} 

public Assets() { 
} 

public int getBook() { 
    return book; 
} 

public void setBook(int book) { 
    this.book = book; 
} 
public int getFromChapter() { 
    return fromChapter; 
} 
public void setFromChapter(int fromChapter) { 
    this.fromChapter = fromChapter; 
} 
public int getToChapter() { 
    return toChapter; 
} 
public void setToChapter(int toChapter) { 
    this.toChapter = toChapter; 
} 
public int getFromVerse() { 
    return fromVerse; 
} 
public void setFromVerse(int fromVerse) { 
    this.fromVerse = fromVerse; 
} 
public int getToVerse() { 
    return toVerse; 
} 
public void setToVerse(int toVerse) { 
    this.toVerse = toVerse; 
} 
public String getCreator() { 
    return creator; 
} 
public void setCreator(String creator) { 
    this.creator = creator; 
} 
public String getDiscription() { 
    return discription; 
} 
public void setDiscription(String discription) { 
    this.discription = discription; 
} 
public String getSource() { 
    return source; 
} 
public void setSource(String source) { 
    this.source = source; 
} 
public String getTitle() { 
    return title; 
} 
public void setTitle(String title) { 
    this.title = title; 
} 
public String getDuration() { 
    return duration; 
} 
public void setDuration(String duration) { 
    this.duration = duration; 
} 
public String getUrl() { 
    return url; 
} 
public void setUrl(String url) { 
    this.url = url; 
} 

PicAsset:

public class PicAsset extends Assets implements IsSerializable { 

    private int picId=0; 

    public PicAsset(){ 
    } 

    public PicAsset(int picId, int book, int fromChapter, int toChapter, 
     int fromVerse, int toVerse, String creator, String discription, 
     String source, String title, String duration, String url) { 
    super( book, fromChapter, toChapter, 
      fromVerse, toVerse, creator, discription, 
      source, title, duration, url); 
    this.picId = picId; 
    } 

    public int getIdpic() { 
    return picId; 
    } 
    public void setIdpic(int idpic) { 
    this.picId = idpic; 
    } 
} 

現在我使用的RPC調用使用服務器端宣佈從我datbase獲取信息的方法,你可以看到方法返回PicAsset,List的列表。

rpcService.getPicture((books.getSelectedIndex()+1), (chapters.getSelectedIndex()+1), new AsyncCallback<List<PicAsset>>(){ 
        public void onFailure(Throwable caught) { 
         Window.alert("Can't connect to database" + books.getSelectedIndex() + chapters.getSelectedIndex()); 
        } 

        public void onSuccess(List<PicAsset> result) { 
         int listSize = result.size(); 
         int i; 
         int flag = 0; 

         assetPicPanel.clear(); 

          Label frameTitle = new Label("Pictures"); 
           for(i=0;i<listSize;i++) 
           { 
            if(flag == 0) 
            { 
             assetPicPanel.add(frameTitle); 
             flag = 1; 
            } 


            HorizontalPanel vPanelPic = new HorizontalPanel(); 

            System.out.print("heeeeey" +" " + result.get(i).getFromChapter());          
            Grid g = result.get(i).AssetGrid(result.get(i)); 

            vPanelPic.add(g); 
            assetPicPanel.add(vPanelPic); 
           } 
        } 
       }); 

現在,當我在服務器端打印..get()。getFromChapter()時,它會帶來正確的值。 但是,當我打印已返回到RPC調用的值時,我得到默認的構造函數值...而不是必須發回。

在這裏還要在服務器端的Getpicture中實現:

public List<PicAsset> getPicture(int book, int chapter) throws Exception 

{ 

    System.out.print("getPicture ok " + book +"," + chapter); 
    Connection conn = null; 
    PreparedStatement pstmt = null; 
    ResultSet result = null; 
    List<PicAsset> relevantAssets = new ArrayList<PicAsset>(); 
    PicAsset relAsset; 


    try { 
     conn = getConnection(); 
     pstmt = conn.prepareStatement("SELECT * FROM picasset WHERE book = ? AND fromChapter <= ? AND toChapter >= ?"); 

     //System.out.print("connection" + conn); 
     pstmt.setInt(1, book); 
     pstmt.setInt(2, chapter); 
     pstmt.setInt(3, chapter); 

     result = pstmt.executeQuery(); 

     // System.out.print(result); 
     while (result.next()) { 

      //System.out.print("in while"); 
      relAsset = new PicAsset(result.getInt("picId"),result.getInt("book"), result.getInt("fromChapter"), result.getInt("toChapter"),result.getInt("fromVerse"),result.getInt("toVerse"),result.getString("creator"),result.getString("discription"),result.getString("source"),result.getString("title"),result.getString("duration"),result.getString("url")); 
      relevantAssets.add(relAsset); 

     } 
    } 

     catch (SQLException sqle) 
     { 
      sqle.printStackTrace(); 
     } 
     finally 
     { 
      // Cleanup 
      result.close(); 
      pstmt.close(); 
      conn.close(); 
     } 

    System.out.print("In MySql get Chapter " + relevantAssets.get(0).getFromChapter() + " " + relevantAssets.get(0).getIdpic()); 
    return relevantAssets; 
} 

回答

0

在GWT RPC這將是更好的使用原始數組,而不是集合接口 - 從你的方法PicAsset []而不是List回報。這將允許你(a)解決你的問題,並(b)逃避不必要的類被編譯成客戶代碼。

請參見「裸類型」在gwtproject.org

+1

部分沒有什麼錯誤使用集合,只要你使用特定的實現,像ArrayList的''。 – 2014-10-04 18:28:40

+0

我認爲這是一個繼承問題,我錯過了這裏的東西,我不知道它是什麼... 如果我刪除Asset類並實現PicAsset類中的所有東西,則返回的值是可以的。但是我不想重複代碼... – 2014-10-04 18:52:57

+0

嘗試將IsSerializable移動到父類中和/或將其替換爲JDK的Serializable。 – ursa 2014-10-04 18:54:28