PrimeFaces's Dialog Framework (DF) can be used to open a dialog from our Managed Bean. Additionally, we can select the content of the dialog dynamically rather than declaring <p:dialog /> statically. RequestContext#openDialog() is used to open such dialog.
Example
JSF page
src/main/webapp/index.xhtml<h:body>
<h2>PrimeFaces - Programmatic Dialog</h2>
<h:form>
<br/>
<p:commandButton value="Show Employees" actionListener="#{mainBean.showEmployees}"/>
</h:form>
</h:body>
The manage bean
@ManagedBean
public class MainBean {
public void showEmployees(ActionEvent ae) {
RequestContext.getCurrentInstance().openDialog("employee", getDialogOptions(), null);
}
public Map<String, Object> getDialogOptions() {
Map<String, Object> options = new HashMap<>();
options.put("resizable", false);
options.put("draggable", true);
options.put("modal", true);
options.put("height", 400);
options.put("contentHeight", "100%");
return options;
}
}
The Dialog Content Page
src/main/webapp/employee.xhtml<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Employees</title>
</h:head>
<h:body>
<h:form>
<p:dataTable var="employee" value="#{employeeBean.employeeList}">
<p:column headerText="Id">
<h:outputText value="#{employee.id}"/>
</p:column>
<p:column headerText="Name">
<h:outputText value="#{employee.name}"/>
</p:column>
<p:column headerText="Phone Number">
<h:outputText value="#{employee.phoneNumber}"/>
</p:column>
<p:column headerText="Address">
<h:outputText value="#{employee.address}"/>
</p:column>
</p:dataTable>
</h:form>
</h:body>
</html>
The Employee bean
@ManagedBean
@ViewScoped
public class EmployeeBean {
private List<Employee> employeeList = new ArrayList<>();
@PostConstruct
private void postConstruct () {
DataFactory dataFactory = new DataFactory();
for (int i = 1; i < 10; i++) {
Employee employee = new Employee();
employee.setId(i);
employee.setName(dataFactory.getName());
employee.setPhoneNumber(String.format("%s-%s-%s", dataFactory.getNumberText(3),
dataFactory.getNumberText(3),
dataFactory.getNumberText(4)));
employee.setAddress(dataFactory.getAddress() + "," + dataFactory.getCity());
employeeList.add(employee);
}
}
public List<Employee> getEmployeeList () {
return employeeList;
}
}
faces-config.xhtml
For Dialog Framework to work, we need to add following configurations in faces-config.xhtml.
src/main/webapp/WEB-INF/faces-config.xml<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-facesconfig_2_1.xsd"
version="2.1">
<application>
<action-listener>
org.primefaces.application.DialogActionListener
</action-listener>
<navigation-handler>
org.primefaces.application.DialogNavigationHandler
</navigation-handler>
<view-handler>
org.primefaces.application.DialogViewHandler
</view-handler>
</application>
</faces-config>
To try examples, run embedded tomcat (configured in pom.xml of example project below):
mvn tomcat7:run-war
Output
Example ProjectDependencies and Technologies Used: - primefaces 6.1 primefaces
- jsf-api 2.2.14:
This is the master POM file for Oracle's Implementation of the JSF 2.2 Specification.
- jsf-impl 2.2.14:
This is the master POM file for Oracle's Implementation of the JSF 2.2 Specification.
- datafactory 0.8: Library to generate data for testing.
- JDK 1.8
- Maven 3.3.9
|