File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSF and the fly likes How can I close a JSF page from the Java Bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "How can I close a JSF page from the Java Bean" Watch "How can I close a JSF page from the Java Bean" New topic

How can I close a JSF page from the Java Bean

Efrat Bar-Nahum
Ranch Hand

Joined: Jan 19, 2006
Posts: 57

I have a JSF window (dialog).
When the user presses the OK button I want to submit an action in the java bean.
After that I want to close the window.

What I do today is:

I guess there are 2 options:
1. To close the window (window.close()) from the jsf page itself after the submit, but I don't know how.
2. To do it from the add method in the java bean, but I also don't know how to do it...

Can anyone help me?
Thomas Walters

Joined: Jul 15, 2009
Posts: 2
I found the below text at another site (

Closing the popup window

If the close button has a "cancel" semantic, we can use a simple OutputLink:
<h:outputLink onclick="window.close(); return false;" value="#">
<h:outputText value="cancel" />

The window will simply close. The server will not notice anything.
Submit form and close the popup window

For a proper action handling which goes through the whole request lifecycle we need to use an action listener:
<h:commandLink actionListener="#{popupBean.closeWindowClicked}" value="submit and close" />
public void closeWindowClicked(ActionEvent event) {
FacesContext facesContext = FacesContext.getCurrentInstance();

String javaScriptText = "window.close();";

// Add the Javascript to the rendered page's header for immediate execution
AddResource addResource = AddResourceFactory.getInstance(facesContext);
addResource.addInlineScriptAtPosition(facesContext, AddResource.HEADER_BEGIN, javaScriptText);

Note: If there is a validation error the window will not close unless the "immediate" attribute of the commandLink is set to "true".

hope that helps,
I agree. Here's the link:
subject: How can I close a JSF page from the Java Bean
It's not a secret anymore!