Introduction to JSF (Java server faces) framework


Before going forward we should know about Model 1 and Model 2 Architecture.

Model 1 Architecture

In this Architecture all kinds of requirements get fulfilled in one layer. The user provides a request to the JSP and the JSP provides a response to the user. This Architecture uses a set of JSP to implement presentation logic, business logic, controlling logic. It is suitable for small-scale requirement due to its simplicity. It does not provide reusability.

Model 2 Architecture

This layer is divided into three parts, they are as below

  1. Model
  2. Controller
  3. View

Model: - This layer implements the business logic and contains data. The state of users can be stored in this layer. This layer can be created by using a set of java beans.

Controller: - This layer implements the controlling logic. It decides what views go to which users' requests. This layer can be created by using a servlet and a set of helper classes. The user gives a request to the servlet present in the layer.

View: - This layer implements the presentation logic of the application. It provides a response to the user. A set of JSP can be used to create this layer.

Introduction to Java server faces

Model: - JSP implement business logic with the help of some java beans called managed bean. The manage bean contains states of the application through the properties and validates users input to create appropriate navigation case for the controlling layer.

Controller: - This layer contains only one servlet to accept request from users provides a predefined servlet known as the faces servlet.

View: - This layer can be created in JSF by using a set of JSP or x.html files. JSF provides a set of tags to support MVC based presentation.

Installation of JSF

  1. Get the copy of the JSF binary distribution file (Mojarra-2.0.2-fcs-binary.zip) and extract into any folder of the file system. Get the file from the below link
    http://www.4shared.com/file/fU8yFzet/mojarra-202-FCS-binary.html
     
  2. Find JSF-api.jar and JSF-impl.jar inside the   'lib' folder of the extracted folder and make those two files available in the   'lib' folder of the context.

Configuration of JSF

  1. Create a configuration file named faces-config.xml in WEB-INF folder. This file contains information about all managed bean and navigation cases.

    Tips

    Search for faces-config.xml inside the extracted folder of JSF. Copy all of this file and make it available inside WEB-INF folder of the context. Open this file and remove all the tags present in between <Faces-config> and </Faces-config>
     
  2. Provide the name of the configuration file to the context by using <context-param> in the web.xml file. Deploy the controlling servlet by using the required tag in web.xml. The controlling servlet must be a permanent servlet.

Web.xml file

<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<web-app 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_2_5.xsd"
version="2.5">
<context-param>
<param-name>javax.faces.CONFIG_FILES</param-name>
<param-value>/WEB-INF/faces-config.xml</param-value>
</context-param>
<servlet>
<servlet-name>x</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup> 1 </load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>x</servlet-name>
<url-pattern>*.JSF</url-pattern>
</servlet-mapping>
</web-app>

Tag library of JSF

  1. Core tag library: - This provides a facility to render or convert JSF tags to HTM tags.

    Uri:http://java.sun.com/JSF/core
     
  2. Html tag library: - This provides a facility to create MVC based HTML tags.

    Uri:http://java.sun.com/JSF/html

Creating JSF based JSP

  1. Create a file by using .JSP extension.
  2. Include the core and html tag libraries by using their uri into the tag lib directive.
  3. Use the view tag of the core tag library as the parent tag of all JSF tags.
  4. Use the required JSF tags to make the content visible to the user.
  5. Access the JSP by using extension of the controlling servlet (x.JSF).

Creation of a context file

Create any folder in any drive as (E:\JSF). Inside that folder store your .JSP files. Give the Context path name as javaJSF and docBase as E:\JSF, here docBase means the total path where we are storing our .JSP files. Store the java bean file inside 'classes' folder of the context for predefined context (root) the classes folder required to be created inside the WEB-INF folder. These changes are done in the server.xml file, which is present in (E:\Program Files\Apache Software Foundation\Tomcat 6.0\conf) directory.

Running first application in JSF

Create a file named as hello.JSP in the context folder.

hello.JSP

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


<html>
<body>
<f:view>
<h1><h:outputText value="Hello, Welcome to JSF 1.2 World!"/></h1>
</f:view>
</body>
</html>

web.xml settings

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app 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_2_5.xsd"
version="2.5">
<context-param>
<param-name>javax.faces.CONFIG_FILES</param-name>
<param-value>/WEB-INF/faces-config.xml</param-value>
</context-param>
<servlet>
<servlet-name>x</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup> 1 </load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>x</servlet-name>
<url-pattern>*.JSF</url-pattern>
</servlet-mapping>
</web-app>

Running the application

Run the tomcat then write the below line in the URL
http://localhost:8081/javaJSF/hello.JSF

Here javaJSF is the Context path, which we mentioned in the server.xml file, which is present in (E:\Program Files\Apache Software Foundation\Tomcat 6.0\conf) directory.

Output

JSF.gif

Up Next
    Ebook Download
    View all
    Learn
    View all