2012-03-28 19 views
0

我裏面有一個數據表中的fileDownload成分,但是當我點擊它,它似乎之前datlis.filepath變量由setPropertyActionListener設置filedownloader被調用。廣東話實例講座號碼:filedownload˚F之前調用:setPropertyActionListener

當我點擊下載,我得到「廣東話實例化類:ui.FileDownloader.com.sun.faces.mgbean.ManagedBeanCreationException:廣東話實例類:ui.FileDownloader。」

我的JSF代碼:

<p:column headerText="Metadata" style="width:40px"> 
    <p:commandButton id="selectButton" rendered="#{datlis.has_metadata}" icon="ui-icon-circle-arrow-s" title="View" ajax="false" > 
     <f:setPropertyActionListener value="#{datlis.filepath}" target="#{filedownloader.filepath}" /> 
     <p:fileDownload value="#{filedownloader.file}" /> 
    </p:commandButton> 
</p:column> 

那datlis'代表我的應用程序ViewScoped的豆 - 我已經檢查了datlist.filepath不爲空。並將該文件下載豆(FileDownloader)如下:

@ManagedBean(name="filedownloader") 
@RequestScoped 
public class FileDownloader { 

private StreamedContent file; 

public StreamedContent getFile() { 
    return file; 
} 

@ManagedProperty(value="#{param.filepath}") 
private String filepath; 

public String getFilepath() { 
    return filepath; 
} 
public void setFilepath(String filepath) { 
System.out.println("> "+filepath); 
    this.filepath = filepath; 
System.out.println(">> "+this.filepath); 
} 

public FileDownloader() throws FileNotFoundException { 
System.out.println("100"); 
    String filename = "/opt/glassfish3/glassfish/domains/domain1/datasets/string_compare/Business v2 Metadata/README.txt"; 
     InputStream stream = new FileInputStream(filepath); 
     file = new DefaultStreamedContent(stream, "text/txt", "README.txt"); 
} 

堆棧跟蹤提到了一個空指針異常有關的InputStream,這就是爲什麼我想在「文件路徑」變量沒有被設置 - 再加上我的系統輸出只顯示來自該的System.out.println「100」,並從setFilepath功能沒有系統的輸出...,如果它不是在所有調用。

我也試過:

<p:column headerText="Metadata" style="width:40px"> 
       <p:commandButton id="selectButton" rendered="#{datlis.has_metadata}" icon="ui-icon-circle-arrow-s" title="View" ajax="false" > 
        <f:param name="filepath" value="#{datlis.filepath}" /> 
        <p:fileDownload value="#{filedownloader.file}" /> 
       </p:commandButton> 
      </p:column> 

只有在我FileDownloader類的文件路徑的getter/setter上面添加的代碼:

@ManagedProperty(value="#{param.filepath}") 
private String filepath; 

但是,這也似乎並沒有工作。有任何想法嗎?我覺得我在正確的軌道或許只是濫用元素上...

回答

1

託管屬性將施工後注入。所以如果你嘗試在bean的構造函數中訪問它們,你會得到一個NPE。

使用帶有@PostConstruct註釋的方法。它會自動建築及物業注射後調用:

@PostConstruct 
public void init() { 
    // do your initializations here 
} 
+0

感謝我用了F:PARAM電話與我filedownloader功能@PostConstruct,加入了void返回值,它的定義,它看起來像它的工作原理 – Alaph432 2012-03-29 02:06:39