|
|
JSF server side user interface component framework for Java™ technology-based web applications.Java Server Faces (JSF) is an industry standard and a framework for building component-based user interfaces for web applications.
JSF contains an API for representing UI components and managing their state; handling events, server-side validation, and data conversion; defining page navigation; supporting internationalization and accessibility; and providing extensibility for all these features.
The major benefits of JavaServer Faces technology are:
- Java Server Faces architecture makes it easy for the developers to use. In JavaServer Faces technology, user interfaces can be created easily with its built-in UI component library, which handles most of the complexities of user interface management.
- Offers a clean separation between behavior and presentation.
- Provides a rich architecture for managing component state, processing component data, validating user input, and handling events.
- Robust event handling mechanism.
- Events easily tied to server-side code.
- Render kit support for different clients
- Component-level control over statefulness
- Highly pluggable - components, view handler, etc
- JSF also supports internationalization and accessibility
- Offers multiple, standardized vendor implementations
In a nutshell, Faces has the following advantages over Struts:
- Eliminated the need for a Form Bean
- Eliminated the need for a DTO Class
- Allows the use of the same POJO on all Tiers because of the Backing Bean
The primary advantages of Struts as compared to JavaServer Faces technology are as follows:
- Because Struts is a web application framework, it has a more sophisticated controller architecture than does JavaServer Faces technology. It is more sophisticated partly because the application developer can access the controller by creating an Action object that can integrate with the controller, whereas JavaServer Faces technology does not allow access to the controller. In addition, the Struts controller can do things like access control on each Action based on user roles. This functionality is not provided by JavaServer Faces technology.
- Struts includes a powerful layout management framework, called Tiles, which allows you to create templates that you can reuse across multiple pages, thus enabling you to establish an overall look-and-feel for an application.
- The Struts validation framework includes a larger set of standard validators, which automatically generate both server-side and client-side validation code based on a set of rules in a configuration file. You can also create custom validators and easily include them in your application by adding definitions of them in your configuration file.
The greatest advantage that JavaServer Faces technology has over Struts is its flexible, extensible UI component model, which includes: [list] A standard component API for specifying the state and behavior of a wide range of components, including simple components, such as input fields, and more complex components, such as scrollable data tables. Developers can also create their own components based on these APIs, and many third parties have already done so and have made their component libraries publicly av
The main implementations of Java Server Faces are: - Reference Implementation (RI) by Sun Microsystems.
- Apache MyFaces is an open source JavaServer Faces (JSF) implementation or run-time.
- ADF Faces is Oracle’s implementation for the JSF standard.
A typical JSF application consists of the following parts:
- JavaBeans components for managing application state and behavior.
- Event-driven development (via listeners as in traditional GUI development).
- Pages that represent MVC-style views; pages reference view roots via the JSF component tree.
JavaServer Faces applications are just like any other Java web application. They run in a servlet container, and they typically contain the following:
- JavaBeans components containing application-specific functionality and data.
- Event listeners.
- Pages, such as JSP pages.
- Server-side helper classes, such as database access beans.
In addition to these items, a JavaServer Faces application also has:
- A custom tag library for rendering UI components on a page.
- A custom tag library for representing event handlers, validators, and other actions.
- UI components represented as stateful objects on the server.
- Backing beans, which define properties and functions for UI components.
- Validators, converters, event listeners, and event handlers.
- An application configuration resource file for configuring application resources.
JavaBean objects managed by a JSF implementation are called managed beans. A managed bean describes how a bean is created and managed. It has nothing to do with the beans functionalities.
Backing beans are JavaBeans components associated with UI components used in a page. Backing-bean management separates the definition of UI component objects from objects that perform application-specific processing and hold data.
The backing bean defines properties and handling-logics associated with the UI components used on the page. Each backing-bean property is bound to either a component instance or its value. A backing bean also defines a set of methods that perform functions for the component, such as validating the components data, handling events that the component fires and performing processing associated with navigation when the component activates.
A view object is a model object used specifically in the presentation tier. It contains the data that must display in the view layer and the logic to validate user input, handle events, and interact with the business-logic tier. The backing bean is the view object in a JSF-based application. Backing bean and view object are interchangeable terms.
At the end of the ValueChangeListener, call FacesContext.getCurrentInstance().renderResponse()method
getRequestParameterValuesMap() method is similar to getRequestParameterMap(), but it returns multiple values for the parameters with the same name. This method is useful when using components such as <h:selectMany>
Set redisplay=true, it is false by default.
JSF was developed using MVC (a.k.a Model View Controller) design pattern so that applications can be scaled better with greater maintainability. It is driven by Java Community Process (JCP) and has become a standard. The advantage of JSF is that its both a Java Web user a interface and a framework that fits well with the MVC. It provides clean separation between presentation and behavior. UI (a.k.a User Interface) can be created by page author using reusable UI components and business logic part can be implemented using managed beans.
Following things required for JSF:
- JDK (Java SE Development Kit)
- JSF jars
- Application Server (Tomcat or any standard application server)
- Integrated Development Environment (IDE) like Netbeans, Eclipse, etc.
Once JDK and Application Server is downloaded and configured, one can copy the JSF jar files to JSF project and could just start coding.
You can get a reference to the HTTP request object via FacesContext like this: FacesContext fc = FacesContext.getCurrentInstance(); HttpServletRequest request = (HttpServletRequest) fc.getExternalContext().getRequest(); and then use the normal request methods to obtain path information. Alternatively, context.getViewRoot().getViewId(); will return you the name of the current JSP (JSF view IDs are basically just JSP path names).
Current JSF implementation does not add the context path for outputLink if the defined path starts with /. To correct this problem use #{facesContext.externalContext.requestContextPath} prefix at the beginning of the outputLink value attribute. For example: <h:outputLink value="#{facesContext.externalContext.requestContextPath}/myPage.faces">
If you have the following URL: http://your_server/your_app/product.jsf?id=777, you access the passing parameter id with the following lines of java code:
FacesContext fc = FacesContext.getCurrentInstance(); String id = (String) fc.getExternalContext().getRequestParameterMap().get("id");
|
|
|