jsf 2 - Action link and download link in one -



jsf 2 - Action link and download link in one -

i need show link download file on page in jsf 2 application. now, issue file contains info depends on fresh looks database @ time of creation. want create , give user link download in 1 action.

this simple in 2 actions: have button generate file, , replace link download file 1 time it's generated.

so question 1 time again can done in 1 click on commandlink?

edit, next balusc's comment below. here's specifics of trying do, , have done far. have in xhtml:

<h:panelgroup rendered="#{!bean.showlinktoexcelfile()}"> <h:form> <td><h:commandbutton value="generate list of bids download" action="#{bean.createbidslist()}"/></td> </h:form> </h:panelgroup> <h:panelgroup rendered="#{bean.showlinktoexcelfile()}"> <td><a href="#{bean.findbidslistfilename()}">download bids</a></td> </h:panelgroup>

this works. button creates excel file, saves location, , updates file name in database. link serves file. it's 2 step process user. i'd 1 step. 1 link, example:

<a href="#{bean.findbidslistfilename()}">download bids</a>

or, likely, jsf commandlink create excel file on end, save /resources/ location, , seamlessly open "save" dialog on user's machine.

you can allow jsf write study outputstream of http response instead of outputstream of local disk file system.

here's basic example, assuming you're using apache poi create excel report:

public void createanddownloadbidsreport() throws ioexception { // prepare excel study downloaded. hssfworkbook bidsreport = createbidsreportsomehow(); string filename = "bids.xls"; // prepare response show save dialogue excel report. facescontext facescontext = facescontext.getcurrentinstance(); externalcontext externalcontext = facescontext.getexternalcontext(); externalcontext.setresponsecontenttype("application/vnd.ms-excel"); externalcontext.setresponseheader("content-disposition", "attachment; filename=\"" + filename + "\""); // write excel study response body. bidsreport.write(externalcontext.getresponseoutputstream()); // inform jsf response completed , doesn't have navigate. facescontext.responsecomplete(); }

this way can end single command link (or button).

<h:form> <h:commandlink value="create , download bids report" action="#{bean.createanddownloadbidsreport}" /> </h:form>

note doesn't save disk file system. if really need have re-create you'd need add together bidsreport.write() line writes desired disk file scheme location.

jsf-2 download

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -