Hey, I'm trying to open a pdf in a new window. Anyone with some experience?
My jsp code is:
<h:commandLink value="" actionListener="#{docsProyectoVO.descargaDoc}"
immediate="true">
<h:graphicImage url="#{rsc.btn_pdf}" styleClass="border0"/>
<f:param value="#{documento.idProyecto}" name="idProyecto" />
<f:param value="#{documento.idDocumento}" name="idDocumento" />
</h:commandLink>
The managed bean:
public void descargaDoc (ActionEvent event){
try{
String idP = (String)
getApplication().createValueBinding("#{param.idProyecto}").getValue(getFacesContext());
String idD = (String)
getApplication().createValueBinding("#{param.idDocumento}").getValue(getFacesContext());
if (!this.lstDocumentos.isEmpty() && idP != null &&
!idP.equalsIgnoreCase("")) {
// Obtenemos fichero de bbdd
DocumentoDTO d =
getIProyectoSvc().obtenerDocumento(Long.parseLong(idD));
if (d != null) {
generaDoc(d.getDatosDocumento(), d.getStrNombre(),
d.getStrMime());
}
}
}catch(Exception e){
e.printStackTrace();
}
}
public static void downloadFile(HttpServletResponse response, byte[] bytes,
String fileName, String fileType, boolean attachment) throws IOException {
// Prepare stream.
BufferedOutputStream output = null;
try {
// Prepare.
int contentLength = bytes.length;
String disposition = "attachment";
// If content type is unknown, then set the default value.
// For all content types, see:
// http://www.w3schools.com/media/media_mimeref.asp
if (fileType == null) {
fileType = "application/octet-stream";
}
// Init servlet response.
response.setContentLength(contentLength);
response.setContentType(fileType);
response.setHeader("Content-disposition", disposition + ";
filename=\"" + fileName + "\"");
//response.addHeader("Content-Disposition",
"inline;filename="+fileName);
output = new BufferedOutputStream(response.getOutputStream());
output.write(bytes);
// Finalize task.
output.flush();
} finally {
// Gently close stream.
if (output != null) {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
// This is a serious error. Do more than just printing a
// trace.
}
}
}
The first problem is h:commandLink without immediate='true' does't get de
ActionListener. When immediate='true' a get the documet open at the same
page...
I am navigating.
My configuration:
* jsf-facelets
* myfaces 3.2.2
* jboss 4.2
Thanks for all.... ;)