JavaServer Faces java sun com javaee javaserverfaces Contents 1 Introduction 2 Application Configuration 3 The Standard Components 4 Internationalization 5 Messaging 6 The Request Processing Lifecycle 7 Converters and Validators 8 Custom Tags 9 Custom Components 10 Composite Components 11 Facelets Templating 12 AJAX Support 2 1 Introduction Web Frameworks Automatic markup generation Declarative integration of backing beans UI component model Server side handling of UI events JSF Type conversion Navigation Struts Form handling and validation Extensible templating mechanism Integration with Java session management error handling HTTP request response JSP Facelets Servlets Web server 4 The greatest advantage that JSF technology has over Struts is its flexible extensible component model which includes An extensible component API for the usual standard components Developers can also create their own components based on the JSF APIs and many third parties have already done so and have made their component libraries publicly available e g MyFaces http www myfaces org A separate rendering model that defines how to render the components in various ways For example a component used for selecting an item from a list can be rendered as a menu or a set of radio buttons An event model that defines how to handle events generated by activating a component such as what to do when a user clicks a button or a hyper link Automatic conversion and validation 1 Introduction JSF Scope 5 Web browsers don t know anything about JSF components or events When a user clicks a button in a JSF application it causes a request to be sent from your web browser to the server or more precisely to the FacesServlet JSF is responsible for translating that request into an event that can be processed by your application logic on the server usually by the backing bean JSF is also responsible that every UIComponent you have used on your pages is properly displayed on your browser JSF applications run on the server and can integrate with other subsystems like EJBs web services or databases There is a central configuration file for JSF applications usually called faces config xml Here we can define the supported locales the used backing beans navigation rules custom validators or converters and so on In the new JSF2 0 standard many of these configurations are replaced by annotations in the Java code 1 Introduction A simple JSF Page html xmlns http www w3 org 1999 xhtml xmlns h http java sun com jsf html h head title Hello World title h outputStylesheet name styles css h head h body h form id main h outputText value Hello World styleClass header table tr td h outputText value Your Name td td h inputText id helloInput value helloBean name td tr tr td h outputText value Your Age td td h inputText id helloAge value helloBean age td tr table h commandButton value Say Hello action helloBean sayHello p h outputText value helloBean helloText h form h body html 6 This is an example of a simple page with labels input text fields and a submit button Say Hello For the web page designer a JSF page looks similar to a normal HTML page We have to define the necessary namespaces JSF core JSF html so that the JSF tags can be used These pages can also be designed by an IDE like NetBeans IntelliJ Sun Java Studio Creator IBM WebSphere or Oracle JDeveloper 1 Introduction Translation to HTML html xmlns http www w3 org 1999 xhtml head link type text css rel stylesheet href title Hello World title head body form id main name main method post span class header Hello World span table tr td Your Name td td input id main helloInput type text name main helloInput value Felix td tr tr td Your Age td td input id main helloAge type text name main helloAge value 25 td tr table input type submit name main j idt16 value Say Hello p Good Morning Felix form body html 7 At runtime the FacesServlet automatically translates the JSF tags to HTML OutputText tags are converted to normal text inputText tags to a HTML input tag and the submit button to a HTML input tag with type submit Hidden input fields are used to transmit state information about the client or the server 1 Introduction JSF Application Structure 8 Developing a simple JSF application usually requires the following tasks Create the JSF Pages Develop the model object business logic Develop the backing beans with the properties as well as the used action methods The application configuration file Faces Config can be used to declare the backing beans and to define the page navigation In the JSF pages we use the unified expression language EL expressions to bind UI component values and objects to backing bean properties or to reference backing bean methods The Faces Servlet is provided by the JSF application and is responsible for the communication between the server and the clients Front Controller pattern 1 Introduction JSF Concepts 9 This simplified UML class diagram shows the different concepts of JSF The view object contains a tree of UIComponents output labels command buttons text fields that are displayed by the dedicated renderers On user input the backing beans are automatically updated Converters translate and format the component s value and generate an error message if necessary Validators verify the value of a component and generate an error message if necessary In general JSF applications communicate by events Backing beans contain event listeners and action methods The outcome of an action method is used to specify the next page to be shown navigation Event listeners consume events and can execute model objects which perform the core application logic In JSF applications model objects don t know anything about the user interface This enforces a MVC style architecture Confer chapter 2 2 of http jsfatwork irian at 1 Introduction The JSF Expression Language Access bean property or method myBean myProperty myBean myMethod Access for array list element or map entry myBean myList 5 myBean myMap key 10 The Expression Language uses the number sign to mark the beginning of an expression EL expressions can be two way they can be used to retrieve a property value or to update it EL expressions can also reference object methods Confer chapter 2 5 of http jsfatwork irian at 1 Introduction The JSF Expression Language Boolean Expression myBean myValue 100 myBean myValue 200 not empty myBean myValue Usage h inputText value myBean clientName validator myBean check rendered not empty myBean myValue 11 1
View Full Document
Unlocking...