I have a JSP page containing a h:dataTable
. The datatable has a column of h:commandLink
components which open a new popup window. These commandLink components have an actionListener
method in the page's backing bean. The code determines which commandLink component was clicked, grabs its parameter and redirects to a servlet. The servlet writes a file to the popup window and displays the "save as/open" dialog box so user's can download the file written. This all works fine.
However, after closing the popup window, if I click on a JSF button on the page, I get the "save as/open" dialog box again. How do I prevent this dialog box from reappearing? I noticed this doesn't happen if Page1.jsp
is refreshed before the JSF button is clicked.
Here is the code:
Page1.jsp
<noscript>
<!-- Page1.jsp -->
<webuijsf:button actionExpression="#{Page1.btnSubmit_action}" id="btnSubmit" text="Apply"/>
<h:column id="column9">
<h:commandLink value=" Open " target="popupWindow" actionListener="#{Page1.openPopupClicked}" >
<f:param id="tmpFileId" name="id" value="#{currentRow['J_LINK']}" />
</h:commandLink>
<h:outputLink target="_blank" value="#{currentRow['J_LINK']}"/>
<f:facet name="header">
<h:outputText id="outputText18" value="More "/>
</f:facet>
</h:column>
</noscript>
Page1
backing bean
public void openPopupClicked(ActionEvent event){
UIParameter tmpFileName = (UIParameter)event.getComponent().findComponent("tmpFileId");
if(tmpFileName==null)
return;
String fName = (String)tmpFileName.getValue();
if(fName==null)
return;
final String viewId = "/FileDisplayerServlet";
HttpSession hs = this.getHttpSession();
hs.setAttribute("tmpToShow", fName);
this.redirectToServlet(viewId);
}
Servlet code called by processRequest method:
private void printFileToScreen(HttpServletRequest request,String tmpFileToShow, HttpServletResponse response)
throws IOException{
ServletOutputStream sos = null;
FileInputStream in = null;
try{
response.reset();
response.setContentType(getContentType(tmpFileToShow));
if(currFileExt==null)
return;
String fileName = "document.".concat(currFileExt);
sos = response.getOutputStream();
response.setHeader("Content-disposition", "attachment; fileName="+fileName);
File src = new File(tmpFileToShow);
in = new FileInputStream(src);
byte[] buf = new byte[1024];
int len =0;
response.setHeader("Cache-Control", "private");
while((len = in.read(buf, 0, buf.length)) > 0){
sos.write(buf, 0, len);
}
}catch(IOException ie){
System.out.println("printFileToScr: "+ie.toString());
}finally{
if(sos!=null)
{sos.flush(); sos.close();}
if(in!=null)
{in.close();}
}
}