Refresh JSF page programmatically from JavaBean

4 08 2012

When business logic in JavaBean decides that forced jsf page refresh is needed, it can be done from logic’s level. Add this code to your logic (prefereably put it in some refresh() method):

FacesContext context = FacesContext.getCurrentInstance();
String viewId = context.getViewRoot().getViewId();
ViewHandler handler = context.getApplication().getViewHandler();
UIViewRoot root = handler.createView(context, viewId);

Browsers may have cache enabled, so some cached elements may not be refreshed. If this is your case, you should also add header to Http response, that will force browser not to use cache:

response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0);

Cached elements were not my problem, so the second snippet is not tested by me. For more info, please see following sources: OTN Discussion Forum, Refresh current JSF page and caching on StackOverflow.

Note: page refresh done that way means sending Http Response – one Http Request can have only one Http Response. You can’t use this code if you have already sent your response earlier for this particular Http Request.

For example – while performing file download. The file download data is sent in Http Response, so unfortunately there is no way to refresh page programmaticaly. Probably Ajax refresh could help you, if just some of the components should be rerendered. This problem is described in threads: StackOverflow thread 1Coderanch question, another Coderanch problem, refresh after response is commited, and the last one Coderanch – so.. just impossible…


.. so maybe better to use this JavaScript delayed reload – give it a try!

Did I help you?
I manage this blog and share my knowledge for free sacrificing my time. If you appreciate it and find this information helpful, please consider making a donation in order to keep this page alive and improve quality

Donate Button with Credit Cards

Thank You!




8 responses

25 08 2012
JavaScript: page refresh after timeout « Looks OK!

[…] logic requires to refresh it after some timeout, the JavaScript function can help. If you use JSF, take a look here to see how to refresh page from Java […]

10 04 2013

thank you very much

12 05 2014

Thanks a lot! This is helpful.

17 06 2014

FacesContext.getCurrentInstance() gives null :-(
how to sorted it

24 09 2014
Claudia Arévalo

Hi….Page refresh after a file download is not imposible. I had the same problem and I solved this way:

response.setHeader(“Content-Disposition”, “attachment; filename=” + getFileName());
response.setHeader(“Refresh”, “3; url = pageToRefresh.jsf”);
ServletOutputStream stream = response.getOutputStream();

The “Refresh” header can be used to reload the desired page after the file download. It can be the same page from which the file was downloaded.

27 09 2014

you saved my time a lot! thanks 1000 times.

21 10 2014

Hi Claudia, it works only when you downlod the file. however, if you press cancel button the page is not refreshed. Any solution for that ?

23 10 2014

this does not work for me ;/

Give Your feedback:

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: