Very simple solution. Use a command button with image.

JSF Code
<h:commandButton value="Download"  styleClass="button"
actionListener="#{employeeList.downloadListener}"
image="/images/excel_icon.gif" />

Generated HTML
<input id="employees:j_id13" name="employees:j_id13"
                                type="image" 
src="/testapp/images/excel_icon.gif"
                                onclick="if(typeof
window.clearFormHiddenParams_employees=='function'){clearFormHiddenParams_employees('employees');}"
                                class="button" />





mjovanov wrote:
> 
> We have a very simple setup for exporting data table content to excel: a
> commandLink invoking an action method that renders content type
> "application/vnd.ms-excel". However we are seeing some really funky
> behavior, specifically whenever actions other than exporting to excel are
> invoked, the exportHtmlTableToExcel action method still gets executed
> during JSF lifecycle, causing the browser to display the Download/Open
> dialog box again! Using the debugger I can see the exportHtmlTableToExcel
> getting invoked even though it should not be. It's as if the JSF state
> somehow got corrupt and/or was not properly reset from the previous cycle
> involving Exporting to Excel. Has anyone seen this before? Any suggestions
> would be greatly appreciated.
> 
> code:
> <h:commandLink action="#{backingBean.exportHtmlTableToExcel}"
> class="linkCaption">
>   <t:graphicImage url="images/icon_excel.gif"
> alt="#{msg.labelExportQueueSummToExcel}" width="21" height="16" />
>   <h:outputText value="#{msg.labelExportToExcel}" />
> </h:commandLink>
> 
> public void exportHtmlTableToExcel() throws IOException {
>     String contentType = "application/vnd.ms-excel";
>     FacesContext fc = FacesContext.getCurrentInstance();
>     String filename = fc.getExternalContext().getUserPrincipal().getName()
> + "-" + System.currentTimeMillis()
>             + ".xls";
>     HttpServletResponse response = (HttpServletResponse)
> fc.getExternalContext().getResponse();
>     response.setHeader("Content-disposition", "attachment; filename=" +
> filename);
>     response.setContentType(contentType);
> 
>     StringBuffer htmlBuffer = new StringBuffer();
>     ...
>     [write data table content to buffer here]
>     ...
>     ...
>     PrintWriter out = response.getWriter();
>     htmlBuffer.append("<HTML>\n");
>     out.print(htmlBuffer);
>     out.close();
>     fc.responseComplete();
> }
> 
> 

-- 
View this message in context: 
http://www.nabble.com/Export-Data-Table-to-Excel---Strange-JSF-Lifecycle-Behavior-%21-tp17831096p23280575.html
Sent from the MyFaces - Users mailing list archive at Nabble.com.

Reply via email to