Code that works in JSF is not working with Richfaces - pleas
aish Mar 12, 2008 2:15 PMHi,
I have the following code that works fine in JSF. I replaced the <h:commandLink> with <a4j:commandLink> and the code does not work as expected. Can you please guide me why the richfaces code is not working.
Here are the related codes:
=================
1. index.jsp - in the format of jsf that works fine
----------------------------------------------------------
<h:commandLink
id="downloadPdfInline"
value="Download PDF"
action="#{myBean.downloadPdfInline}"
target="_blank"
/>
2. index.jsp - using richfaces commandLink and does not work
-------------------------------------------------------------------------
<a4j:commandLink
id="downloadPdfInline"
value="Download PDF"
action="#{myBean.downloadPdfInline}"
target="_blank"/>
3. 'myBean' Beans and its methods
--------------------------------------------
public class MyBean
{
private String errorString;
/* to open and display an pdf file - hardcoded filename*/
public void downloadPdfInline()
{
// Prepare.
File pdfFile = new File(getPdfFilePath(), getPdfFileName());
// File pdfFile = new File(getPdfFileName());
FacesContext context = FacesContext.getCurrentInstance();
HttpServletResponse response =
(HttpServletResponse) context.getExternalContext().getResponse();
// Download file.
try
{
HttpServletUtil.downloadFile(response, pdfFile, false);
}
catch (IOException e)
{
// Do your error handling thing.
setErrorMessage("Download file failed: " + e.getMessage());
e.printStackTrace();
return;
}
// Prevent other JSF lifecycle phases eventually being invoked.
// Otherwise you can get the following exception:
// java.lang.IllegalStateException: Cannot forward after response has been committed.
context.responseComplete();
}
private String getPdfFileName()
{
// TODO Auto-generated method stub
//return "SummaryReleaseCalendar.xls";
return "defect-01022008.xls";
}
private String getPdfFilePath()
{
// TODO Auto-generated method stub
//return "C:\\JavaProjects\\Run\\WebContent";
return "C:\\FileLoadTest";
}
private void setErrorMessage(String string)
{
errorString=string;
}
This 'downloadPdfInline' method in myBean call another method (HttpServletUtil.downloadFile) which I have not copied here due to space. If you would like to look at it, please let me know and I will copy it here.
1. The way the program works is as we click the "Download PDF" link, it opens a pdf file. This works if the code is in JSF and opens the pdf file when the link is selected. In Richfaces, when the link is selected, it displays some unreadable characters. Can you please let me know what I am missing.
Thanks
Aish