handouts apache myfaces session
play

Handouts Apache MyFaces Session Slide 5 JSF is a new technology - PDF document

Handouts Apache MyFaces Session Slide 5 JSF is a new technology that enables you to create Java-based web apps. It is more than only a new framework/technology; it is a widely adopted industrial standard. Since JSF provides a clear lifecycle


  1. Handouts Apache MyFaces – Session Slide 5 JSF is a new technology that enables you to create Java-based web apps. It is more than only a new framework/technology; it is a widely adopted industrial standard. Since JSF provides a clear lifecycle to process HTTP requests, it is much cleaner and simpler than JSP/Servlet based development. The lifecycle, for instance, takes care of extracting request parameters and copies them to a corresponding JavaBean class. JSF enables you to uses POJO ( Plain Old Java Objects ) during your development process. Those classes are called backing beans. So there is no need to extend framework classes or implement special interfaces. It’s up to you if your class needs to inherit by a class of your domain specific framework or not. JSF provides a set of user interfaces components (UI components) that are itself JavaBeans with special properties. The concept behind the UI components is similar to that what is known by classic GUI frameworks like Swing. Since JSF focuses on web development it provides no fat client components. Instead it ships some generic components like UIInput or UIOutput . It additionally provides some components that have special HTML properties like HtmlSecret or HtmlTextArea extending the generic UIInput component. For adding new behaviour to the framework components, you could provide a custom component that extends some of the generic or standard HTML components. Since the UI components are standard JavaBeans they need to take care of their rendering. It is possible that a component renders itself or better that it delegates the rendering to a special class called Renderer . A set of Renderer classes is called RenderKit . The JSF standard says that each implementation must contain at least a RenderKit for HTML 4.0.1. But it is possible to create a custom RenderKit . Another interesting point is that JSF is able to handle events that are generated from the web GUI (the UI components). There are currently two concrete events ( ActionEvent and ValueChangedEvent ). Similar to the event concept in the JavaBean standard, JSF provides Listeners for each event. And again, it is possible to create custom events, since the two concrete event classes extend the abstract FacesEvent class, which is itself a specialization of the java.util.EventObject . Beyond these possibilities, you can call your back-end data by special Command components like HtmlCommandButton or HtmlCommandLink . These command components are used during the workflow process to present pages to the browser. For its management of managed-beans , JSF uses the dependency injection pattern that is know from lightweight IoC containers like Spring or Hivemind. It uses one special dependency injection pattern, the setter injection pattern. With the usage of setter injection it is possible to preconfigure properties of your backing beans. Additional to backing beans the “ Managed Bean Facility ” of JSF can also initialize and create other objects like DAO objects, for instance. A very interesting point of JSF is that it contains a concept for converter objects. So you can add a converter to an input field and the input-string is automatically converted to a java.util.Date object. During rendering, it then converts the Date object to a String by

  2. utilizing a pattern for the date format (uses SimpleDateFormat ). So the DateTimeConverter does not! call Date.toString() . There are other convertes like Number - or FloatConverter . Furthermore, it is again possible to create custom converters that map an input-String to your domain specific classes. Slide 6 JSF provides a special lifecycle to process HTTP requests. The lifecycle contains six phases. 1. Restore View A request was sent from the browser to the FacesServlet . It extracts the viewId (page name). The viewId is needed to lookup the components for that specific view. If it is the first request it creates them and wires the converters or event handlers to the components. If the component tree is already created, the FacesServlet reuses it. 2. Apply Request Values JSF retrieves the (submitted) values of the components from the http-request. Those values are “copied” to the their correspondent ui component. 3. Process Validations This phase tries to convert the parameters (which are strings) to the specific type of the UI component property (e.g. Float). After component properties are containing the submitted values, JSF starts to validate these values. Validations can be type dependent, as for instance checking the allowed range of a Float value. To check a Float range, the value must be a Float object, that’s why validation is following the conversion phase. 4. Update Model Values After JSF has checked the validity of all component properties’ values it is time to update the model. During this phase JSF copies the component bound values to the properties of the backing beans, which are wired to a corresponding UI component. 5. Invoke Application After the backing beans contain their desired data values it is time to use them to execute the business layer. JSF calls an action method of a backing bean which can in turn perform some logic on your back-end (e.g. a business delegate). The action method returns a String which is used to calculate the next page being displayed to the user. 6. Render Response This is the time to present the response to the browser. Slide 7 Here we see a JSP page, containing JSF components. Particularly, a form-component, containing two input fields and a button, is displayed. The input fields are bound to properties of a backing bean. The button is bound to a method of the backing bean. Slide 8 Here is the corresponding backing bean class. You see a very simple and clean JavaBean object containing two properties and its setter/getter methods. Those properties are filled by

  3. the JSF lifecycle. After the properties are initialized, you could use them inside of the action method ( send() ). Action methods must be public, no-argument methods and need to return a String. The String is used inside of the application workflow to present pages to the client. Slide 9 Here a simple but complete XML configuration file is displayed. It contains the Managed Bean Facility , which creates the backing beans on demand. It also contains the workflow area of a JSF application. The settings are such that when the send() method returns fine, the browser will display the output.jsp file. Slide 10 So what are the reasons for using JSF, since there are more frameworks that do something similar? Popular web frameworks are for instance Struts and Tapestry. A big point is that JSF is an industrial standard and it will be part of the next J2EE version. So every J2EE 5 server must ship an implementation of the JSF specification. That means there will be a big vendor support, not only on server side. Most common IDEs will support the standard as well; there are already many IDEs on the market which support JSF development one way or another. Furthermore, there will be a 3 rd party market for UI components. Slide 12 MyFaces was “born” as inhouse application in Austria (Europe). The “founders” decided to make it public as open source. Since 2005 MyFaces is a toplevel project of Apache Software foundation. MyFaces 1.1.x has passed the TCK for JSF 1.1.!!! Slide 14 Since MyFaces is a complete JSF specification implementation it provides an implementation of the API and of the implemention classes. Furthermore, it provides lot of custom components and extensions. MyFaces also supports the JSR-168 portlet standard. MyFaces provides a lot of jars. You could have each artefact in a single jar file or also an all- in-one jar file. It’s up to you. And note, that MyFaces provides lot’s of example files! Slide 15 Using MyFaces in your web application is easy; there is near nothing that needs to be configured. Perhaps you need to add a special ServletContextListener to your web.xml file and all will be fine! The ServletContextListener uses Jakarta Digester to read the configuration file and inits the needed JSF infrastructure. If you would like to use some of the extensions it will be good to have a special ServletFilter nested in you’re your web.xml file. The Filter adds images and JavaScript files that are needed by some of MyFaces custom components. Also it helps you to upload files with MyFaces. Note the performance of the Filter is good! Slide 16

Recommend


More recommend