私はdownload.jsfのようなJSFでのファイルのダウンロードを提供?ID = 123
http://www.domain.example/download.jsf?id=123
と私の解決策のスタイルで(それはまた、imgタグで画像のために働く必要があります)のダウンロードリンクを提供しようとしています(ここからコピー:How to provide a file download from a JSF backing bean?)
GetFile.java
@ManagedBean
@ViewScoped
public class GetFile{
// property and getter
public void setId(String id) {
FacesContext fc = FacesContext.getCurrentInstance();
ExternalContext ec = fc.getExternalContext();
ec.responseReset();
ec.setResponseContentType(contentType);
ec.setResponseContentLength(contentLength);
ec.setResponseHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
try (OutputStream output = ec.getResponseOutputStream()) {
// Now you can write the InputStream of the file to the above
// OutputStream the usual way.
// ...
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
fc.responseComplete();
}
と私のこれまでのところ、このようになります。 download.xhtmlは次のようになります。
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core">
<f:metadata>
<f:viewParam name="id" value="#{getFile.id}" />
</f:metadata>
</html>
これまでのところ動作します。私は
http://www.domain.example/download.jsf?id=123&format=txt
のようなパラメータを追加したい場合しかし、私は、パラメータが設定される順序を考慮して、最後のセッターでこれを行う必要があります。それはうまくいくだろうが、私は非常にきれいな解決策を見つけられないので、私の質問は、これを達成するためのより良い方法があるということですか?
どのようなヒントもありがとうございます!