<?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:f="http://java.sun.com/jsf/core" > <h:head> <title>Phone Directory Search</title> </h:head> <h:body> <h:form id="directoryForm"> <h2>Phone Directory Search</h2> Enter person name : <h:inputText id="name" value="#{phoneDirectoryBean.name}" /> <p/> <h:commandButton actionListener="#{phoneDirectoryBean.search}" value="Get Phone Number"> <f:ajax execute="@form" render="directoryForm:phone"/> </h:commandButton> <p/> <h3><h:outputText value="#{phoneDirectoryBean.phone}" id="phone"/> </h3> </h:form> </h:body> </html>
import java.util.TreeMap; import javax.faces.bean.ManagedBean; import javax.faces.event.ActionEvent; @ManagedBean // phoneDirectoryBean is taken as the default name. Request is the default scope. public class PhoneDirectoryBean { private TreeMap<String,String> directory = new TreeMap<String,String>(); private String name,phone; public PhoneDirectoryBean() { directory.put("Mickey", "9898989898"); directory.put("Donald","9999999999"); directory.put("Jerry", "9090909090"); } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public void search(ActionEvent evt) { phone = directory.get(name); if ( phone == null) { phone = "Sorry! Name not found"; } } }
<web-app version="3.0" 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-app_3_0.xsd"> <context-param> <param-name>javax.faces.PROJECT_STAGE</param-name> <param-value>Development</param-value> </context-param> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.xhtml</url-pattern> </servlet-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> <welcome-file-list> <welcome-file>phonesearch.xhtml</welcome-file> </welcome-file-list> </web-app>