PrimeFaces – Open Window By Dropdown Selection

Technology: Primefaces it’s java based web framework to develop web applications using java.
It’s one of the compliance frameworks for JSF, there are many other frameworks like omnifaces, richfaces. Primefaces has rich UI components.

In this tutorial primefaces, maven, java8, glasifish servers are used.

Use case: If you want to open a new page by clicking on button, link or something it’s straight forward thing. Even if the page URL is addressed from directly backing bean we can do it. But if the page URL is dynamic, it’s not a straight forward thing because by the time you click on the button, the page is already rendered, so you will have old value. So, in this case I am going to implement this use case.

There are several steps to implement this,

  1. Create a Primefaces maven project, download glassfish and add it in the server part of eclipse.
  2. Deploy the application and test.

1. Project Structure

2. Project Dependencies

I just added primefaces, jsf library.


<project xmlns="" 
           <!-- -->  
           <!-- -->  
           <!-- -->  

3. Code

Have created a Bean, Converter and Enum and index.xhtml(facelet)

import java.util.Arrays;  
import java.util.List;  
import javax.faces.bean.ManagedBean; 
public class MyBean {  
     private static final String GOOGLE = "";  
     private static final String YAHOO = "";  
     private WebOptions selectedOption;  
     private String webOptionUrl;  
     public String getWebOptionUrl() {  
           return webOptionUrl;  
     public void setWebOptionUrl(String webOptionUrl) {  
          this.webOptionUrl = webOptionUrl;  
     public void prepareUrl(){  
               webOptionUrl = GOOGLE;  
          } else if(WebOptions.YAHOO.equals(selectedOption)) {  
               webOptionUrl = YAHOO;  
          } else {  
               webOptionUrl = "";  
     public List<WebOptions> getAllWebOptions() {  
          return Arrays.asList(WebOptions.values());  
     public WebOptions getSelectedOption() {  
          return selectedOption;  
     public void setSelectedOption(WebOptions selectedOption) {  
          this.selectedOption = selectedOption;  

import javax.faces.component.UIComponent;  
import javax.faces.context.FacesContext;  
import javax.faces.convert.Converter;  
import javax.faces.convert.FacesConverter;  
import org.omnifaces.cdi.ViewScoped;  

@FacesConverter(value = "", forClass = WebOptionConverter.class)  
public class WebOptionConverter implements Converter, Serializable {  
     /** the serialVersionUID      **/  
     private static final long serialVersionUID = -218581226063576481L;  
     public WebOptionConverter(){  
     public Object getAsObject(FacesContext context, UIComponent component, String value) {  
          return WebOptions.getOptionByOptoin(value);  
     public String getAsString(FacesContext context, UIComponent component, Object value) {  
          if(value instanceof WebOptions){  
               final WebOptions objectStatus = (WebOptions) value;  
               return objectStatus.getWebOption();  
          return "";  


public enum WebOptions {  
     private String webOption;  
     WebOptions(String webOption){  
          this.webOption = webOption;  
     public static WebOptions getOptionByOptoin(String value){  
               return WebOptions.GOOGLE;  
          return WebOptions.YAHOO;  
     public String getWebOption() {  
          return webOption;  

<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  
<html xmlns=""  
     <script type="text/javascript">  
    function openUrl(){  
       var webOptionUrl = document.getElementById("websiteForm:webOptionHidden").value;  
       if(webOptionUrl.length === 0){  
            var message = 'Please select website';  
       } else {  
  , '', 'width=1024, height=720, status=no, scrollbars=1 menubar=no, toolbar=no');  
     <h:form id="websiteForm">  
          <p:panelGrid id="selectOption" style="margin-left:500px;">  
                         <p:outputLabel value="Please select website: "></p:outputLabel>  
                         <p:selectOneMenu id="option" value="#{myBean.selectedOption}">  
                              <f:selectItem itemLabel="--" itemValue="" />  
                              <f:selectItems value="#{myBean.allWebOptions}" var="o" itemLabel="#{o.webOption}" itemValue="#{o}"/>  
                                   <p:ajax update="@form" process="@this" listener="#{myBean.prepareUrl}"></p:ajax>  
                              <f:converter converterId="" />  
                    <p:commandButton id="submitButton" value="Submit" oncomplete="openUrl();" update="webOptionHidden"/>  
                    <p:inputText id="webOptionHidden" value="#{myBean.webOptionUrl}" style="display:none;"/>  

<?xml version="1.0" encoding="UTF-8"?>  
<web-app xmlns:xsi="" 
	id="WebApp_ID" version="3.0">  


4, Demo

Build the application and add it to the server and run the application.

4.1 If you run the application, the screen will look like this,

4.2 If you don’t select any option, it will prompt you to select an option.

4.3 For now I have added two pages google and yahoo, if you select any of them, it’s corresponding website will open in another window.

4.4 Now I will explain the trick, hope you are familiar with primefaces widgets, omnifaces conveter, backing bean… If you are not, I recommend you please go through primefaces tutorial.

  1. Let’s say didn’t select any option and clicked on submit, javascript method will be checked where I am checking whether any option is selected or not, if nothing selected just simply asking the user select any of the options.
  2. Let’s say you selected any of the options, now an Ajax call we invoked, causes the selected option will set to the backing bean and prepares it’s respective page URL. After that, (if you observe facelet, index.xhtml I have added a text field which will not be shown on the web page)
  3. After ajax call completed, this text field will be updated, I mean this text field will have the selected web option.
  4. If you click on the submit now, again the javascript method will be called, there we are taking the value from this text field, so it contains the URL of the option. So the URL will be opened in new window.

Experts Java development programmers team have shared their best knowledge about java technology and its use in java project. If you need to get more information, you can ask the developers who are already applying this technology in their projects.

Read More Related This :
JPA optimistic lock exception in Java Development
This post explains the JPA technology and its use in java development. Experts of java development India are explaining the use case of technologies- JPA and Hibernate, MySql database, Maven. Read this post and know what they want to say.


  1. PrimeFaces – Ultimate UI Framework for Java EE
author image

Johnny Morgan

Johnny Morgan as a technical writer at Aegis Infoways, leading Java Development India since more than 6 years. I write articles especially for Java, Python and Asp.Net. I have also got well response to write articles on CRM, Hadoop and QA Read all published posts by


newest oldest most voted

The page is web.xml and not Web.xhtml right ?


Nice Article…thanks for sharing