Published on

Introduction to JSF frame work

Published in: Technology, Education
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. Java Server FacesJSFDone by: Esraa M YaseenSubmitted to:Eng. AbdelnasserAbdelhadi
  2. 2. MVCBecause it provides a flexiblesolution to these problems bydecoupling theModel, View, and Controllercomponents of anapplication while providing auniform interface betweenthem.
  3. 3. MVC …
  4. 4. Web application frameworkA web application framework (WAF) is a softwareframework that is designed to support thedevelopment of dynamic websites, webapplications, web services and web resources. Theframework aims to alleviate the overheadassociated with common activities performedin web development. For example, manyframeworks provide librariesfor database access, templating frameworksand session management, and they oftenpromote code reuse. For a comparison of concreteweb application frameworks
  5. 5. Make a decision!If: You wish to provide different representations of the same applicationdata (for example, a table versus a graph). You wish to provide different looks and feels (perhaps for differentoperating systems) for your user interface without affecting the rest ofyour application. User-driven events must immediately update application data or otheruser interface components, while changes to application data must bereflected immediately in user interface components. You wish to reuse one or more user interface components indepen-dently of application data.Then … Use JSF 
  6. 6. JSF Main featuresJSF has the following main features: JSF is based on the Model-View-Controller concept JSF has a stateful UI component model, e.g. eachcomponent is aware of its data JSF separates the functionality of a component from thedisplay of the component.The renderer is responsible of displaying the component fora certain client. This renderer can get exchanged. Thestandard renderer for JSF components is the HTML renderer. JSF support listeners on UI components JSF support data validation, data binding and dataconversion between the UI and the model
  7. 7. The greatest advantage that JavaServerFaces technology has over Struts is itsflexible, extensible UI componentmodel, which includes: A standard component API for specifying the state andbehavior of a wide range of components, including simplecomponents, such as input fields, and more complexcomponents, such as scrollable data tables. Developerscan also create their own components based on theseAPIs, and many third parties have already done so andhave made their component libraries publicly available. A separate rendering model that defines how to render thecomponents in various ways. For example, a componentused for selecting an item from a list can be rendered as amenu or a set of radio buttons. An event and listener model that defines how to handleevents generated by activating a component, such aswhat to do when a user clicks a button. Conversion and validation models for converting andvalidating component data.
  8. 8. Prerequisites to use JSFTo use JSF you need: JSF Implementation (in the form of the JSFjars) The JSTL tags library A Java runtime environment A web-container to use JSF in (for exampleTomcat)
  9. 9. RequirementsInstallation EclipseFor JSP development you need the Eclipse WTPand an installed Tomcat. JSF libraryA JSF library is required. We will later use Eclipseto download and install the Apache MyFacesJSF implementation during project creation. JSLT library
  10. 10. The Genius of JSF1* A component model allows you to reuse components that comefrom a third party withouthaving to learn a new component model (the model is standard).2* components fit into a part/whole hierarchy and can be composedinto more complex components.The component-based model is beneficial to the handling of the useraction.3* A user action, such as a button click, flows through a well-definedprocess from the button click to the business logic that performs therequested process.JSF provides well-defined points for your code to plug into the flow andbe executed.4* The component-based model also allows developers to focus onproviding great features instead of trying to focus on two or threedifferent models for building Web-based user interfaces. It also allowsthe promise of reusable off-the-shelf components in the future.
  11. 11. JSF configuration filesOverviewJSF is based on the following configuration files: web.xml - General web application configuration file faces-config.xml - Contains the configuration of the JSF application. web.xml*JSF requires the central configuration list web.xml in the directory WEB-INFof the application. This is similar to other web-applications which arebased on servlets.*You must specify in web.xml that a "FacesServlet" is responsible forhandling JSF applications. "FacesServlet" is the central controller for theJSF application. "FacesServlet" receives all requests for the JSF applicationand initializes the JSF components before the JSP is displayed. faces-config.xml"faces-config.xml" allows to configure the application, managedbeans, convertors, validators, and navigation.
  12. 12. What typical JSF applicationconsists of? A typical JSF application consists of thefollowing parts: JavaBeans components for managingapplication state and behavior. Event-driven development (via listeners as intraditional GUI development). Pages that represent MVC-style views; pagesreference view roots via the JSF componenttree.
  13. 13. ControllerThe JSF controller consists primarily of a FrontController servlet called FacesServlet, one ormore configuration files, and a set of actionhandlersThe FacesServlet is responsible for receivingincoming requests from Web clients andthen performing a logical set of steps forpreparing and dispatching a response.
  14. 14. JSF Request-Processing LifeCycle.
  15. 15. Example
  16. 16. NotesThe first thing you’ll notice here i: Our action handler here is simply a method in aJavaBean that has no parameters and returns aString. We chose toplace this method in our LoginBean. In more complex applications, action handlersmay be organized differently. A JSF action handler returns a logical result(success or failure in this case), while a Struts Action uses the logical result toactually return what page should be forwarded tonext.
  17. 17. Example .. configuration file As part of the request-processing life cycle, thenext component tree (or JSP page in this example) will be determined by thelogical result returned by this action handler. As with Struts, JSF allows you todefine a configuration file. Among other things, this configuration fileprovides a mechanism for defining user interface workflow. An example of what theworkflow for our example might look like is provided next...
  18. 18. configuration file
  19. 19. Model This distinction between the View andModel layers of a Web application.becomes more difficult in JSF. must resist the temptation to let JSFcomponent data objects influence yourreal Model (business objects) Failing to do so often results in highcoupling between the View and Modellayers
  20. 20. What is Managed Bean? JavaBean objects managed by a JSFimplementation are called managedbeans. A managed bean describes howa bean is created and managed.
  21. 21. Declaring a Model object as amanaged beanOnce declared in this manner, each managed bean and its declaredproperties can be referenced and bound to user interface components
  22. 22. View As you have probably figured out by now, theView layer in JSF consists primarily of thecomponent tree. One benefit of JSF is thatindividual components or the wholecomponent tree can be rendered differentlyto support multiple client user interface types.In most cases, you will be dealing with amarkup language such as HTML that is used ina JSP. Depending on the client device type,components could render themselves in themarkup language appropriate for that device.
  23. 23. View … What is view object? A view object is a model object usedspecifically in the presentation tier. Itcontains the data that must display in theview layer and the logic to validate userinput, handle events, and interact withthe business-logic tier. The backing beanis the view object in a JSF-basedapplication. Backing bean and viewobject are interchangeable terms.
  24. 24. In thiscase, wecan bindthe inputfields topropertieson ourLoginBeanJavaBean.
  25. 25. Note You could have made use ofConverters, Validators, and even customRenderers as part of the user interface. Infact, a Converter and Validator componentwould be useful even in this simple example.These components are very useful fordelegating and reusing common userinterface tasks. In the interest of simplicity, wedecided not to use these special JSFcomponents in our littlelogin example.
  26. 26. Composite Components The primary component is usually aform, frame, or page. Within these rootcomponents, other components arearranged hierarchically to achieve thedesired interface.
  27. 27. Composite Components• AbstractComponent. This participantdefines an interface common to allcomponents. It implements this commoninterface to provide defaultbehavior, a portion of which is used foraccessing and managing nestedcomponents.• SimpleComponent. This participantrepresents and defines the behaviorof primitive components.• CompositeComponent. Thisparticipant represents and defines thebehavior of container components. Itprovides a mechanism for storingand managing nested components byimplementing those portions of• the AbstractComponent interface.Client. This participant accesses andmanipulates component hierarchiesthrough the AbstractComponentinterface.
  28. 28. Composite Components When a Client performs anoperation on a component, auniform response is providedregardless of the underlyingstructure. If the component isa SimpleComponent, theoperation is performeddirectly. Otherwise, we have aCompositeComponent that askseach nested component toperform the requestedoperation. TheCompositeComponent mayperform additional operationsbefore responding to the Client.In either case, these interactionsare hidden beneath a commoninterface.
  29. 29. JSF EventsJSF provides two kinds of events: A Value Changed event(javax.faces.event.ValueChangeEvent) is useful forobserving node or changing the text in a text field). An Action event (javax.faces.event.ActionEvent ) isuseful for observing the activation of a user inter-facecomponent that is a descendent of UICommand(this includes buttons andhyperlinks). Both of these event types ultimately descend from acommonancestor in JSF (javax.faces.event.FacesEvent).
  30. 30. JSF EventsThere are essentially two steps to capturing events in JSF. The first step is to implement the appropriate listener interface onthe component you wish to receive an event. When implementingthe ValueChangeListener interface, you’ll need to add aprocessValueChanged(...) method where you will implement thecode that responds to the event. Similarly, when implementingthe ActionListener interface, you’ll need to add a processAction(...)method. When an event is dispatched to your listenercomponent, these methods will be called by the JSF implementation. The second step to capturing an event in JSF is to register yournewly created listener.
  31. 31. Examples of JSF event handling<h:inputText id=”userId”value=”#{login.userId}”><f:valueChangeListenertype=”logindemo.UserLoginChanged” /></h:inputText><h:commandButton id=”login”commandName=”login”><f:actionListenertype=”logindemo.LoginActionListener” /></h:commandButton>
  32. 32. You may be wondering if it is possibleto combine some aspects of Strutswith JSF ?component framework with existing Struts-based applications. The creators of Strutshave created an integration library calledStruts-Faces that is available on the Strutsproject Web site that does just that.
  33. 33. Summary
  34. 34. References John Wiley and Sons - Mastering Java Server Faces http://www.vogella.com/articles/JavaServerFaces/article.html http://www.developersbook.com/jsf/interview-questions/jsf-interview-questions-faqs.php http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse.jst.jsf.doc.user/html/tasks/create_jsf_app.html http://myfaces.apache.org/jsfintro.html http://www.mkyong.com/jsf2/jsf-2-0-hello-world-example/ http://www.coderanch.com/t/212078/JSF/java/JSF