java - Using a commandButton in a jsf Page to download a file -
using commandbutton in jsf page download file. using: jsf & richfaces.
i have table (extends extendeddatamodel implements modifiable, serializable) data , in each row button "download".
<a4j:commandbutton id="getdownload" value="download" style="margin-left:10px;margin-right:10px;width:100px;" action="#{controller.download}" immediate="true" ajaxsingle="true"> <f:setpropertyactionlistener target="#{controller.idstring}" value="#{item.id}" /> </a4j:commandbutton>
i have build file in controller:
public void download(){ outputstream out = null; .... facescontext fc = facescontext.getcurrentinstance(); httpservletresponse response = (httpservletresponse) fc.getexternalcontext().getresponse(); out = response.getoutputstream(); zipoutputstream zipout = new zipoutputstream(out); ..... zipout.close(); response.setcontenttype("application/octet-stream"); response.addheader("content-disposition", "attachment; filename=\""+filename+"\""); out.flush(); .... } { try { if (out!=null){ out.close(); } facescontext.getcurrentinstance().responsecomplete(); } catch (ioexception e) { logger.error(e); } } ... }
the problem started, when implemented extendeddatamodel self. @ first used h:commandlink, controller method never called... tried , tried... correct method called, (zip) file content displayed in page. want button/link in page, user can click download file. page should not change. ideas?
i can create servlet, not understand why extendeddatamodel changed behavior of links inside.
edit1
i used
<h:commandlink id="getdownload" value="download" action="#{controller.download}"> <f:setpropertyactionlistener target="#{controller.idstring}" value="#{item.id}" /> </h:commandlink>
before. works "normal" richfaces table, not when used inside own table extends extendeddatamodel.
edit 2 - solution/workaround
it impossible use h:commandbutton, .. link... whatever inside of self made table, download file. using 1 button in table render new panelgroup , second button inside of new panelgroupt download file. googled lot this, seems rich faces bug.
you can't download file ajax request. replace a4j:commandbutton
h:commandbutton
.
update per question update: command links/buttons inside uidata
component such <h:datatable>
, <rich:datatable>
, etc work, need ensure bean holding data model (whatever behind value
attribute of uidata
component) preserving same data model during request of form submit. if want keep bean request scoped, easiest way reference bean in <a4j:keepalive>
tag.
Comments
Post a Comment