February 8, 2011

Java Server Faces (JSF) - Hello World Example Code

Jars to be added to the lib folder of the Web Application


commons-beanutils.jar
commons-collections.jar
commons-digester.jar
commons-logging.jar
jstl.jar
standard.jar
jsf-api.jar
jsf-impl.jar



web.xml

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xmlns="http://java.sun.com/xml/ns/javaee" 
         xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
         id="WebApp_ID" version="2.5">
         
  <servlet>
      <servlet-name>FacesServlet</servlet-name>
      <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
  </servlet>
  
  <servlet-mapping>
      <servlet-name>FacesServlet</servlet-name>
      <url-pattern>/faces/*</url-pattern>
  </servlet-mapping>

</web-app>


faces-config.xml

<faces-config version="1.2" 
     xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:xi="http://www.w3.org/2001/XInclude"
     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_1_2.xsd">
     
   <managed-bean>
       <managed-bean-name>personBean</managed-bean-name>
       <managed-bean-class>info.icontraining.PersonBean</managed-bean-class>
       <managed-bean-scope>request</managed-bean-scope>
       <managed-property>
  <property-name>personName</property-name>
  <property-class>java.lang.String</property-class>
  <value>DefaultName</value>
       </managed-property>
   </managed-bean> 
    
   <navigation-rule>
       <from-view-id>/inputName.jsp</from-view-id>
       <navigation-case>
   <from-outcome>greeting</from-outcome>
   <to-view-id>/greeting.jsp</to-view-id>
       </navigation-case>
   </navigation-rule>

</faces-config>


PersonBean.java

package info.icontraining;

public class PersonBean {
   
    private String personName;
    public String getPersonName() {
        return personName;
    }

    public void setPersonName(String name) {
        personName = name;
    }

}



inputName.jsp

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>

<html>
 <head>
   <title>Input Name</title>
 </head>
 <body>
   <f:view>
     <h1>
      <h:outputText value="JSF Hello World Example"/>
     </h1>
     <h:form id="helloForm">
       <h:outputText value="Tell us your name:"/>
       <h:inputText value="#{personBean.personName}" />
       <h:commandButton action="greeting" value="Submit" />
     </h:form>
   </f:view>
 </body>
</html>

greeting.jsp

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>

<html>
  <head>
    <title>greeting page</title>
  </head>    
  <body>
     <f:view>
      <h3>
   <h:outputText value="Welcome" />,
   <h:outputText value="#{personBean.personName}" />
         <h:outputText value="!" />
     </h3>
     </f:view>
 </body>
</html>


Type the following URL in the browser:


http://localhost:8080/JSFWebApp/faces/inputName.jsp

No comments:

Post a Comment