SETTING UP JSF

If you have knowledge on java web applications it is very easy to set up Java server faces application.If you have  normal web application follow the below steps to convert it into a jsf app.
  1. An entry in the web application's web.xml to process all the requests by JSF controller servlet called Faces Servlet and a certain URL pattern is specified.

   
         <!-- Faces Servlet -->
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup> 1 </load-on-startup>
    </servlet>


    <!-- Faces Servlet Mapping -->
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.faces</url-pattern>
    </servlet-mapping>


      2 . A JSF configuration file faces-config.xml, which allows for configuration of all elements of JSF     application.It is usually located in the web application's WEB-INF/directory.We need to keep an entry int the web.xml.We will discuss the contents later.
    <context-param>
        <param-name>javax.faces.CONFIG_FILES</param-name>
        <param-value>/WEB-INF/faces-config.xml</param-value>
    </context-param>



     3 . Keep the below jar files in WEB-INF/lib
  • Actual JSF libraries : jsf-api.jar and jsf-impl.jar.
  • Additional dependencies : commons-beanutils.jar,commons-collections.jar,commons-digester.jar and commons-logging.jar
  • JSTL jar files : jstl.jar and standard.jar.


With above steps our web application is properly configured for JSF.
Next step is to develop a view using JSF.
  1. Create a page HelloJSF.jsp in the web application.
  2. Add the below lines in the jsp page to make it JSF compliance.These are core and html tag libraries from sun's reference implementation.
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>


     3. In the body of the JSP,you must add a tag.This is the base UI component .All the jsf tags must be inside this view tag.
Following are the sample files of web.xml and a jsp page ::


Web.xml
<?xml version="1.0"?>
<!DOCTYPE web-app PUBLIC
  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>

    <context-param>
        <param-name>javax.faces.CONFIG_FILES</param-name>
        <param-value>/WEB-INF/faces-config.xml</param-value>
    </context-param>

    <!-- Faces Servlet -->
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup> 1 </load-on-startup>
    </servlet>


    <!-- Faces Servlet Mapping -->
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.faces</url-pattern>
    </servlet-mapping>
</web-app>



   
JSP Page


<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<html >
    <head>
        <title>My First JSF Application</title>
              
    </head>
    <body>
                  <f:view>
                       <h:outputText value="This is MY JSF.."/>                                            
                 </f:view>
       
    </body>
</html>
   


 Once the JSP is created then we need to deploy that application in server and enter the below URL in the browser.
http://localhost:8080//HelloJSF.faces
 Note ::  We need to give the name as HelloJSF.faces eventhogh its exentsion is JSP to wake up the FacesServlet.The servlet container uses the servlet mapping rule to activate the JSF servlet, which strips off the faces suffix and loads the HelloJSF.jsp page.   


 This is all about a sample JSF application .we will discuss in depth in the later posts.







Comments

Popular posts from this blog

What's New in JAVA 8

Singleton Design Pattern

Internals of JSF