2012-03-28 24 views
0

私はdataTableの中にfileDownloadコンポーネントを持っていますが、それをクリックすると、datown.filepath変数がsetPropertyActionListenerによって設定される前に呼び出されたようです。Cantインスタンス化クラス-p:filedownload前に呼び出されるf:setPropertyActionListener

ダウンロードをクリックすると、「Cant instantiate class:ui.FileDownloader.com.sun.faces.mgbean.ManagedBeanCreationException:Cant instantiate class: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がnullでないことをチェックしました。そして、ファイルのダウンロード豆(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"); 
} 

スタックトレースは、入力ストリームに関するnullポインタ例外を言及し、私は「ファイルパス」変数が設定されていないと思っています理由です - プラス私のシステム出力はSystem.out.printlnからの "100"のみを表示し、setFilepath関数からのSystem出力はまったく呼び出されません。

私も試してみた:ちょうど私のFileDownloaderクラスのファイルパスゲッター/セッターの上に追加のコードで

<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> 

を:

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

しかし、これも動作するようには思えません。何か案は?私はおそらくちょうど要素を悪用する正しい軌道にいると感じています...

答えて

1

管理されたプロパティは、建設後に注入されます。したがって、Beanのコンストラクタでそれらにアクセスしようとすると、NPEが取得されます。

@PostConstructと注釈された方法を使用してください。私はFを使用

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

感謝:それは自動的に建設し、財産の注射後に呼び出されます、それの定義にボイド戻り値を加算し、私のfiledownloader機能上の@PostConstructとのparam呼び出しを、そしてそれが動作するように見えます – Alaph432

関連する問題