How to open a new browser window from my JSF page?

By: Carsten Kaiser Emailed: 1604 times Printed: 2076 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

The article describes different ways to open a new browser window from a JSF page and the involved advantages and drawbacks.

Using the "target" attribute of the commandLink component

<h:commandLink target="popupWindow" action="navigateToPopup" actionListener="#{bean.linkClicked}" value="open popup" />

This approach has many drawbacks:

  • In FireFox a new tab is opened instead of a new browser window.

  • The browser window is opened although there are validation errors.

  • Validation errors are displayed in the newly opened window which is very confusing.

  • The appearance of the new window cannot be controlled (e.g. hiding of the location bar)

If no model update is needed then setting the attribute "immediate" to "true" can be used to work around the validation problems.

Note: This is the only way to open a new browser window without using Javascript.

Using an OutputLink and Javascript

<h:outputLink onclick="window.open('popup.faces', 'popupWindowName', 'dependent=yes, menubar=no, toolbar=no'); return false;" value="#">
        <h:outputText value="open popup" />
</h:outputLink>

With this solution we got control over the appearance of the new browser window. And since there is no postback, there is no validation at all. This is the easiest way to open a new browser window when no model update is needed and no action has to be executed.

Advances techniques

In order to implement a proper action handling we need to move the decision whether to open a new window to the action listener.

<h:commandLink actionListener="#{bean.openPopupClicked}" value="open popup" />
public void openPopupClicked(ActionEvent event) {
    // code to open a new browser window goes here
}

How to initiate a Javascript call from server side?

Well, unfortunately there's no standardized way to do so yet. However there are two ways to initiate a Javascript call from server side:

  • Create a JSF page containing the script and navigate to that page. (Will not be described in this tutorial.)

  • Use the Tomahawk's ExtestionFilter to inject the Javascript in the rendered page. (Needs an initial setup but is easier to maintain afterwards.)

Information about how to setup the Tomahawk's ExtensionFilter can be found here: [WWW] http://myfaces.apache.org/tomahawk/extensionsFilter.html

NOTE: The following code example will only work, if the DefaultAddResource or similar implementation is configured to be used, which does not attempt to write to the ResponseWriter immediately (like e.g. StreamingAddResource class) but caches the resources until the render response phase is completed! (see Javadoc of class DefaultAddResource for details)

Now we can inject the Javascript using Tomahawk's ExtensionFilter AddResource class:

public void openPopupClicked(ActionEvent event) {
    // View's id in the same form as used in the navigation rules in faces-config.xml
    // This value could be passed as parameter (using <f:param>)
    final String viewId = "/popup.faces"; 
    
    FacesContext facesContext = FacesContext.getCurrentInstance();

    // This is the proper way to get the view's url
    ViewHandler viewHandler = facesContext.getApplication().getViewHandler();
    String actionUrl = viewHandler.getActionURL(facesContext, viewId);
    
    String javaScriptText = "window.open('"+actionUrl+"', 'popupWindow', 'dependent=yes, menubar=no, toolbar=no');";
    
    // Add the Javascript to the rendered page's header for immediate execution
    AddResource addResource = AddResourceFactory.getInstance(facesContext);
    addResource.addInlineScriptAtPosition(facesContext, AddResource.HEADER_BEGIN, javaScriptText);         
}

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" />
</h:outputLink>

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".


JSF Home | All JSF Tutorials | Latest JSF Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(3)


1. View Comment

It's a useful explanation, but i wanna known how can i do this you comment in the acticle: "Create a JSF page containing the script and navigate to that page. (Will not be described in this tutorial.)", can you explain me that, please, thank you

View Tutorial          By: Noe Marin at 2008-01-25 13:27:57
2. View Comment

in my application i have got several command link, and each one of them should open a popup. When i click on the link though the new popup content is loaded in the previous popup. In this way i'm able to open just one popup. How do i get more than one popup opening when i click on the link? thanks

View Tutorial          By: Carlo at 2009-09-04 04:48:23
3. View Comment

tell me the new page create in java and attach with other

View Tutorial          By: muhammad nadeem at 2011-07-06 00:12:08

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Carsten Kaiser
How to open a new browser window from my JSF page?

More Tutorials in JSF
Struts Vs JSF (A comparison of Struts against JSF)
faces-config.xml to DirectTraffic in the JSF Application
<convertNumber> and <convertDateTime> in JSF
Action listeners in JSF
Calling Multiple Listeners in JSF
Using Javascript in JSF
How to open a new browser window from my JSF page?
Servlet error : java.lang.IndexOutOfBoundsException (JSF RI 1.1_01: IndexOutOfBoundsException)
JSF - TreeNode.setID gets IllegalArgument Exception
Install and Deploy JBoss Application Server
What is JSF (JavaServer Faces)?
Differences between JavaServer Faces technology and Struts
JSF Basics
The Relationship Between JSF and Other Java EE Technologies
JSF Life Cycle

More Latest News
Most Viewed Articles (in JSF )
How to open a new browser window from my JSF page?
Install and Deploy JBoss Application Server
Struts Vs JSF (A comparison of Struts against JSF)
JSF Basics
Installing JSF
faces-config.xml to DirectTraffic in the JSF Application
Action listeners in JSF
Calling Multiple Listeners in JSF
What is JSF (JavaServer Faces)?
Differences between JavaServer Faces technology and Struts
The Relationship Between JSF and Other Java EE Technologies
JSF Life Cycle
Java Bean Scopes in JSF
Controlling Page Navigation in JSF - Static and Dynamic Navigation
Accessing Context Data in Beans using JSF
Most Emailed Articles (in JSF)
What is JSF (JavaServer Faces)?
Accessing Context Data in Beans using JSF
Install and Deploy JBoss Application Server
Java Bean Scopes in JSF
Servlet error : java.lang.IndexOutOfBoundsException (JSF RI 1.1_01: IndexOutOfBoundsException)
Calling Multiple Listeners in JSF
Action listeners in JSF
JSF Life Cycle
faces-config.xml to DirectTraffic in the JSF Application
<convertNumber> and <convertDateTime> in JSF
Differences between JavaServer Faces technology and Struts
Installing JSF
Controlling Page Navigation in JSF - Static and Dynamic Navigation
JSF Basics
How to open a new browser window from my JSF page?