H:CommandLink actionListener 代码在单击 jsf 组件时再次触发
我有一个包含 h:dataTable
的 JSP 页面。数据表有一列 h:commandLink
组件,可打开一个新的弹出窗口。这些commandLink 组件在页面的支持bean 中有一个actionListener
方法。该代码确定单击了哪个 commandLink 组件,获取其参数并重定向到 servlet。 servlet 将文件写入弹出窗口并显示“另存为/打开”对话框,以便用户可以下载写入的文件。这一切都很好。
但是,关闭弹出窗口后,如果单击页面上的 JSF 按钮,则会再次出现“另存为/打开”对话框。如何防止此对话框再次出现?我注意到,如果在单击 JSF 按钮之前刷新 Page1.jsp
,则不会发生这种情况。
下面是代码:
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 代码由 processRequest 方法调用:
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();}
}
}
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();}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论