Issues to use menuitem to export files
rulinsun Mar 19, 2009 5:00 PMI have problem to user menuitems to export files:
the menubar was codes as following:
<rich:toolBar style="width:980px;"> <rich:dropDownMenu style="text-decoration:underline;"> <f:facet name="label"> <h:panelGroup> <h:outputText value="Display"/> </h:panelGroup> </f:facet> <rich:menuItem value="Summary" action="#{controller.showSummary}" > </rich:menuItem> <rich:menuItem value="Split" action="#{controller.showSplit}" > </rich:menuItem> ... </rich:dropDownMenu> <rich:dropDownMenu style="text-decoration:underline;"> <f:facet name="label"> <h:panelGroup> <h:outputText value="Export"/> </h:panelGroup> </f:facet> <rich:menuItem value="Summary" action="#{controller.exportSummary}" > </rich:menuItem> <rich:menuItem value="Split" action="#{controller.exportSplit}" > </rich:menuItem> ... </rich:dropDownMenu> </rich:toolBar>
and backBean Controller.java
private void exportFile(String fileName, String content){ FacesContext fc = FacesContext.getCurrentInstance(); HttpServletResponse response = (HttpServletResponse) fc.getExternalContext().getResponse(); String fileFullName = fileName + ".txt"; response.setHeader("Content-disposition", "attachment; filename=" + fileFullName); response.setContentType(MBSEConstants.TEXT_TYPE); try{ PrintWriter pw = response.getWriter(); pw.write(content); pw.flush(); pw.close(); fc.responseComplete(); } catch(IOException e){ logger.error(StackUtil.getStackTrace(e)); } } public String exportSummary(){ String inFile="summary"; exportFile(inFile, readFile(inFile)); return "success"; }
...
It works fine, when you select any "Export" menuitem for the first time, then when you select another menuitem, the "exportXXX" functions of the early selected menuitems are also executed, the actually exported content is always of the result of the first menuitem ("Summary"). And now when user selects the menuitem in the "Display" dropdown, all the exportXXX functions will also been called.
I am not sure if this is same problem with early post about menuitem being executed twice.
http://www.jboss.org/index.html?module=bb&op=viewtopic&t=150976,
I tried all suggestions in that post, nothing worked. It seems like it relates to the "Export" which actually stays in same view, and displays a popup to allow user to save the file.
Anyone has suggestions?
Thanks!
Rulin