A Technology Blog About Code Development, Architecture, Operating System, Hardware, Tips and Tutorials for Developers.

Tuesday, September 28, 2010

EASY JSF WITH ECLIPSE - BASICS

8:26:00 PM Posted by Satish Kumar , , , , No comments

What is JSF

 JavaServer Faces (JSF) is a UI component based Java Web application framework. JSF is serverbased, e.g. the JSF UI components and their state are represented on the server with a defined life-cycle of the UI components. JSF is part of the Java EE standard.


A JSF application run in a standard web container, for example Tomcat or Jetty . 

A JSF application

A JSF application consists of web pages with JSF UI components. A JSF application requires also some configuration files ("faces-config.xml" and "web.xml").

The faces-config.xml defines:

  1. Managed Bean - the data elements of the JSF application (managed beans and backing beans) Represents a Java class which will be created dynamically during runtime of the JSF application. It can be defined for which scope the bean is valid (Session, Request, Application or none).
  2. the navigation between web pages
  3. data validators - Used to check the validity of UI input
  4. data converters -Used to translate between UI and model
Managed beans are simple Java objects (POJO's) which are declared in "faces-config.xml" and can be used in an JSF application. For example you can define a Java object "Person". Once you define the object in faces-config.xml you can use the attributes of Person in your JSF UI components, e.g. by binding the value "firstName" of this object to an JSF input field.

JSF uses the Unified Expression Language (EL) to bind UI components to object attributes or methods. 

Value and Method Binding

In JSF you can access the values of a managed bean via value binding. For value binding the universal Expression Language (EL) is used (to access bean and / or methods). In JSF you do not need to specify the get() or set() method but just the variable name.

Method binding can be used to bind a JSF component, e.g. a button to an method of a Java class.

  • Expression Language statements either start with "${" or with "#{" and end with "}". JSP EL expressions are using the ${...} syntax. These EL expressions are immediately evaluated. JSF EL expressions are of the type #{...}. These are only evaluated when needed (and otherwise stored as strings).

Prerequisites to use JSF  

To use JSF you need: 

  1. JSF Implementation (in the form of the JSF jars)
  2. The JSTL tags library
  3. A Java runtime environment
  4. A web-container to use JSF in (for example Tomcat)
JSF Main features

JSP has the following main features:

  1. JSP is based on the Model-View-Controller concept
  2. JSP has a stateful UI component model, e.g. each component is aware of its data
  3. JSF separates the functionality of a component from the display of the component. The renderer is responsible of displaying the component for a certain client. This renderer can get exchanged. The standard renderer for JSF components is the HTML renderer.
  4. JSP support listeners on UI components
  5. JSP support data validation, data binding and data conversion between the UI and the model
JSP and JSF 

In this tutorial the JSF application will be build based on JavaServer Pages (JSP's). JSTL tags are used to include JSF UI components into the JSP. This is standard in JSF 1.2. The JSF 2.0 version is using Facelets. 

JSF configuration files

JSF is based on the following configuration files:

  1. web.xml - General web application configuration file
  2. faces-config.xml - Contains the configuration of the JSF application.
web.xml

JSF requires the central configuration list "web.xml" in the directory WEB-INF of the application. This is similar to other web-applications which are based on servlets.

You must specify in "web.xml" that a "FacesServlet" is responsible for handling JSF applications. "FacesServlet" is the central controller for the JSF application. "FacesServlet" receives all requests for the JSF application and initializes the JSF components before the JSP is displayed.

faces-config.xml

"faces-config.xml" allows to configure the application, managed beans, convertors, validators, and navigation.


Sunday, September 26, 2010

EASY JSF WITH ECLIPSE - CONFIGURATION

8:33:00 PM Posted by Satish Kumar , , , , No comments


This tutorial was developed with Java 1.6, JavaServerFaces 1.2, the Apache MyFaces JSF implementation, Tomcat 6.0 and Eclipse 3.5.

Eclipse & Tomcat

For JSP development you need the Eclipse and an installed Tomcat.
JSF library

A JSF library is required. We will later use Eclipse to download and install the Apache MyFaces JSF implementation during project creation.

JSLT library

Download the JSLT library from https://jstl.dev.java.net/.

JSF Project

Create JSF Project

Create a new Dynamic Web Project "JSF". 
Under "Configuration" select "JavaServer Faces v1.2".


Press next until you see the following screen. The JSF and JSTL is showing in the next screen, because I have already configured to my eclipse. For the first time nothing will be there. 


The first time you create a JSF project you need to install / download a JSF implementation. Press the "Download library..." button and select the Apache Library and install it.




Press Manage libraries and create a library for JSTL.



Press new now.


Add the two downloaded JSTL jars and press OK.


Check JSTL and click Finish.



Review the "web.xml" file. It has an entry for the Faces Servlet and for the servlet mapping. Also the file "faces-config.xml" has been created.

Right click on the JSF project and click on Properties. Go to Java Build path -> Libraries and expand the JSF and JSTL library. You can see all the jars associated to the library and the "Publish/ export dependency" shows where the jars has to be placed, when you deploy to the server.


Next step is to add tomcat 6 to eclipse.