File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSF and the fly likes JSF page won't reload if I write to ByteArrayOutputStream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "JSF page won Watch "JSF page won New topic
Author

JSF page won't reload if I write to ByteArrayOutputStream

stephen wills
Greenhorn

Joined: Feb 13, 2010
Posts: 4
Basically, I have a JSF page that displays a count and a "download" button. What I want to have happen is when the button is clicked a text file is created and downloaded and the page's count gets incremented.

When I click the CommandButton the bean.download method gets called, text gets written to a ByteArrayOutputStream and is downloaded. But the count does not refresh (page isn't reloaded).

If I don't call the method to write to the ByteArrayOutputStream, then the page does get updated and shows the new count value.

The JSF page:




The Session bean: Bean




Thanks for any help.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16012
    
  19

Welcome to the JavaRanch, Stephen!

JSF is designed to facilitate HTML data, and especially pages with forms on them. The parts of your webapp that need to output non-HTML data should be implemented in the traditional way using servlets or JSPs. JSF does not require that every part of a webapp must be done exclusively with JSF and the JSF and non-JSF parts of a webapp can freely communicate with each other because the JSF scopes and J2EE scopes are actually the same things, making a JSF session object accessible via the J2EE session API, for example.


Customer surveys are for companies who didn't pay proper attention to begin with.
stephen wills
Greenhorn

Joined: Feb 13, 2010
Posts: 4
Hi Tim,
Thanks for your reply. I'm still a bit unclear of how else I can approach this. I need the JSF button to initiate the download which results in the server returning a file to download. But I also need to show on the web page that the download count has been updated which doesn't happen if the server returns a file. I guess I don't understand what you are telling me. Further advice would sure be welcome.
Cheers
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16012
    
  19

Your download button needs to reference a non-JSF URL (use the h:outputLink tag). That URL should run the Excel file generator that returns the spreadsheet to be downloaded.

Because this kind of operation doesn't navigate away from the JSF screen, you can then use AJAX to update the backing bean containing the download count and re-render the count in the current page display.
stephen wills
Greenhorn

Joined: Feb 13, 2010
Posts: 4
Thanks for your help Tim. I got around the problem by adding to the commandButton to reload the page after one second. Not the best solution but it works.
Cheers
Stephen
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16012
    
  19

That's basically what AJAX does, except that it only updates the part of the page that actually changes instead of the whole thing.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSF page won't reload if I write to ByteArrayOutputStream