2014-10-04 78 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