Download presentation
Presentation is loading. Please wait.
Published byRatna Cahyadi Modified over 5 years ago
1
JavaServer Faces: It is a server side component based user interface framework. It is used to develop web applications. It provides a well-defined programming model and consists of rich API and tag libraries. The latest version JSF 2 uses Facelets as its default templating system.It is written in Java. The JSF API provides components (inputText, commandButton etc) and helps to manage their states. It also provides server-side validation, data conversion, defining page navigation, provides extensibility, supports for internationalization, accessibility etc. The JSF Tag libraries are used to add components on the web pages and connect components with objects on the server. It also contains tag handlers that implements the component tag. With the help of these features and tools, you can easily and effortlessly create server-side user interface.
2
Benefits of JavaServer Faces
1) It provides clean and clear separation between behavior and presentation of web application. You can write business logic and user interface separately. 2) JavaServer Faces API?s are layered directly on top of the Servlet API. Which enables several various application use cases, such as using different presentation technologies, creating your own custom components directly from the component classes. 3) Including of Facelets technology in JavaServer Faces 2.0, provides massive advantages to it. Facelets is now the preferred presentation technology for building JavaServer Faces based web applications.
3
ava Server Faces Versions History Jsf JSF 2.3 Expected in 2017
Release date Description Jsf JSF 2.3 Expected in 2017 It may includes major features: bean validation for complete classes, push communication using enhanced integration with cdi. Jsf 2.2 It has introduced new concepts like stateless views, page flow and the ability to create portable resource contracts. Jsf 2.1 It was a maintenance release 2 of jsf 2.0. only a very minor number of specification changes. Jsf 2.0 It was major release for ease of use, enhanced functionality, and performance. coincides with java ee 6. Jsf 1.2 It has many improvements to core systems and apis. coincides with Java ee 5. initial adoption into java ee. Jsf 1.1 It was a bug-fix release. no specification changes. Jsf 1.0 It was a initial specification released.
4
Architecture of a JSF Application:
A JSF application is made up of a collection of XHTML pages, Java classes, and JSF metadata. The XHTML pages make up the visual part of the web application, holding the various JSF tags , markup, and scripting content that define the UI elements. These Java classes include JavaBeans that hold various parts of the application data, together with Java classes that mediate the data by implementing various types from the JSF APIs, including event handlers, data validators, and data converters The JSF metadata defines how the XHTML pages and JavaBeans are treated in the JSF runtime. A mix of Java annotations and XML documents, this metadata defines the scope of the JavaBeans, how many instances are used, and their lifecycles, together with navigation rules, and can be used to control how the UI components are rendered.
5
Model-View-Controller:
JSF technology is a framework for developing, building server-side User Interface Components and using them in a web application. JSF technology is based on the Model View Controller (MVC) architecture for separating logic from presentation. MVC helps to design an application well and how can we make our web application easy to maintain. The MVC design pattern splits an application design into three separate parts: Model : handles data and logic. View : handles output (presentation) Controller: handles processing of an application.
6
We have created JSF project successfully
We have created JSF project successfully. This project includes following files: index.xhtml: inside the Web Pages directory web.xml: inside the WEB-INF directory Whenever we run the project, it renders index.xhtml as output. Now, we will create an application which contains two web pages, one bean class and a configuration file. It requires the following steps in order to develop new application: Creating user interface Creating managed beans Configuring and managing FacesServlet
7
1) Create User Interface
We will use default page index.xhtml to render input web page. Create a second web page which produce the output. Wihich is jsf page. After creating response.xhtml page. Now, 2) Create a Managed Bean It is a Java class which contains properties and getter setter methods. JSF uses it as a Model. So, you can use it to write your business logic also. 3) Configure Application To configure application, project contains a web.xml file which helps to set FacesServlet instances. You can also set your application welcome page and any more.
8
JavaServer Faces Tags:
The collection of JavaServer Faces tags falls into a number of categories: those for UI components, those for control flow and string manipulation within a page, those to create UI components from other tags, “pass-through” tags and attributes that allow nonstandard markup to be used in a JSF page, and finally the core set of JSF tags that bind everything together. 1.UI components: JavaServer Faces provides rich set of components library to define the architecture of application. It includes the following: Rich set of classes for specifying the state and behavior of user interface components. A rendering model that defines how to render the components in various ways. A conversion model that defines how to register data converters onto a component. An event and listener model that defines how to handle component events. A validation model that defines how to register validators onto a component.
9
JSF Managed Bean: It is a pure Java class which contains set of properties and set of getter, setter methods. Following are the common functions that managed bean methods perform: Validating a component's data Handling an event fired by a component Performing processing to determine the next page to which the application must navigate It also works as model for JFS Framework.
10
JSF Managed Bean Example:
public class User { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } } You can use this bean by the following ways. By configuring into XML file. By using annotations.
11
Configuring Managed Bean into XML file: <managed-bean> <managed-bean-name>user</managed-bean-name> <managed-bean-class>User</managed-bean-class> <managed-bean-scope>session</managed-bean-scope> </managed-bean> Configuring Managed Bean using Annotations: import javax.faces.bean.ManagedBean; import // Using ManagedBean // Using Scope annotation public class User { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
12
annotation in a class automatically registers that class as a resource with the JavaServer Faces. Such a registered managed bean does not need managed-bean configuration entries in the application configuration resource file. This is an alternative to the application configuration resource file approach and reduce the task of configuring managed beans. annotation is used to provide scope for ManagedBean. You can use annotations to define the scope in which the bean will be stored. You can use following scopes for a bean class: Application scope persists across all users? interactions with a web application. Session scope persists across multiple HTTP requests in a web application. View scope persists during a user ?s interaction with a single page (view) of a web application. Request scope persists during a single HTTP request in a web application. None a scope is not defined for the application. Custom user-defined, nonstandard scope. Its value must be configured as a java.util.Map. Custom scopes are used infrequently.
13
f: Core Tags: The JavaServer Faces core tag library contains the basic tags that most JavaServer Faces pages will use. The core tags are contained in the namespace xmlns:f= Most of the tags fall into the three important categories of tags that validate data,in other words, check that the values of data used in a tag fall within certain conditions, tags that manageevents generated in the JavaServer Faces page,and tags that deal with converting data at the page level into a suitable form for consumption by the JavaBeans that back the page.
14
Data Validation: JavaServer Faces provides various levels of validation that you can usethis purpose.Often, it is enough to use the built-in tags for validation, which is the simplest approach and covers the most frequent types of validation. The validator is applied to a tag that takes input by making it a subelement of the tag whose value needs to be checked Tag Function <f:validateLength> Checks the minimum and/or maximum length of an inputstring <f:validateDoubleRange> Checks the minimum and/or maximum value of a input double <f:validateLongRange> Checks the minimum and/or maximum value of a input long <f:validateRequired> Requires some nonempty value to be inpute tag whose value needs to be checked.
15
EXAMPLE: <h:form> <h:inputText id="username_component" validatorMessage="Username must be between 4 and 10 characters"> <f:validateLength minimum="4" maximum="10"/> </h:inputText> </h:form> Input values may also be validated with the Bean Validation API or with regular expressions. Tag Function <f:validateBean> Checks a value against an implementation of the Beans Binding API javax.faces.validator.BeanValidator. <f:validateRegex> Checks a value against a regular expression. Either in the form of a regex expression string: <f:validateRegex pattern="/[0-9a-zA-Z]"/> or as an implementation of the javax.faces.validator. RegexValidator class: <f:validateRegex binding="myRegexValidator">
16
You can also implement your own validator
You can also implement your own public class MyValidator implements Validator { public void validate(FacesContext context,UIComponent component, Object value) throws ValidatorException { // check value // throw ValidatorException if it does not pass the check }
17
event handling tags: event handling tags can be used to register either an instance or a class that implements a certain JavaServer Faces defined event listener interface class. When the tag is used as a subelement of the appropriate user interface component, the event listener they specify is added to that user interface component. When the user interface component’s state changes, the page data is submitted to the Java EE web container, the corresponding event listener is called, and the page regenerated and transmitted back to the browser.
18
Tag Function <f:actionListener> For use with command buttons and links, this tag registers the given implementation of javax .faces.event.ActionListener which is then notified when the component’s state changes. <f:actionListener type ="actionlistener impl classname"> <f:actionListener binding = "expression evaluating to actionlistener instance">
19
<f:valueChangeListener> For use with all input components and lists, this tag registers the given implementation of javax .faces.event.ValueChangeListener, which is then notified when the component’s state changes. <f:valueChangeListener type = "valuechangelistener impl classname"> <f:actionListener binding = "expression evaluating to valuechangelistener instance"> <f:setPropertyActionListener> For use with command buttons and links, this tag lets you directly set the value of a property on a managed bean. <f:setPropertyActionListener target="#{myBean.babyName}" value="#{nameList.currentName}" />
20
Data Converters: JavaServer Faces allows for a number of different kinds of conversions that can cater to several different data types and conversion needs. The starting point for these conversions is the conversion tags in the JavaServer Faces core library. Tag Function <f:convertNumber> Converts a Double or Long object to and from a variety of formats, including standard currency formats, and number formats, specifying ranges for the number of integer and fractional part digits, as well as grouping patterns. <f:convertDateTime> Converts a Date object to and from a variety of humanreadable date formats suitable for a variety of locales and time zones, and in a variety of date formats.
21
JSTL Core Tags We have encountered the Standard Tag Library already. The control flow tags are also available in JavaServer Faces. Here is a summary of these tags and their function. Tag Purpose <c:catch> Catches any throwable thrown within this tag, using the name of the var attribute to hold the throwable. <c:choose> Defines a set of mutually exclusive choices, used in conjunction with the <c:when> and <c:otherwise> tags, like a Java switch statement. <c:if> Simple conditional tag, based on the condition supplied in the test attribute evaluating to true. <c:forEach> Tag for iterating over collections. <c:otherwise> Tag for the default case in a <c:choose> tag. <c:set> Tag to set a page variable by name. <c:when> Simple conditional tag, for use in <c:choose> tags, for example.
22
Extensibility and Modularity:
JavaServer Faces includes many ways to extend its base functionality and to modularize pieces of JavaServer Faces code to promote reuse within an application and across applications. Developers can create custom UI components in two main ways. First, to render a new user interface component on the page, they can provide a new subclass of UIComponent and associate it with a new tag and a new set of control attributes. In this way, many developers have created libraries of new HTML components and custom text components, for example, image components that include mapping functionality.
23
Second, snippets of JavaServer Faces markup may be packaged into an XHTML file as a composite component and associated with a tag using a tag library descriptor. In this way, commonly occurring groupings of tags in JavaServer Faces pages, such as OK cancel button panels and image display panels, can be bundled into a single tag, thereby making it easier to reuse such commonly recurring elements in an application. JavaServer Faces offers a couple of techniques to help web applications that have predefined user interaction pathways. A wizard that collects registration data or a sequence of web forms that help you make and pay for an online purchase are good examples of such interaction pathways. JavaServer Faces defines Faces Flows. This consists of a managed bean scope
24
with the ability to define a set of flows between JavaServer Faces pages and the ability to easily share context, such as a shopping basket or custom registration, between the pages and inject logic into the transitions.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.