Presentation is loading. Please wait.

Presentation is loading. Please wait.

JavaServer Faces framework. 2001 Craig McClanahan is presented that created Struts web framework and based on experience gathered designed JavaServer.

Similar presentations


Presentation on theme: "JavaServer Faces framework. 2001 Craig McClanahan is presented that created Struts web framework and based on experience gathered designed JavaServer."— Presentation transcript:

1 JavaServer Faces framework

2 2001 Craig McClanahan is presented that created Struts web framework and based on experience gathered designed JavaServer Faces framework JavaServer Faces2

3 3 Component-oriented web frameworks

4 JavaServer Faces4

5 How HTML form data are retrieved in a servlet public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {... String age = request.getParameter(“age”); String date = request.getParameter(“date”);... Most data must then be: converted – String must be converted to Date, age must be converted to number validated – age must be positive number JavaServer Faces5

6 We declaratively define what form field must be bind with what class field Web framework: moves data through the binding automatically performs conversion and validation JavaServer Faces6 Approach taken by modern UI frameworks: data binding

7 Backing Beans, JavaBeans spec. Class User shown above is called Backing Bean It is non-visual component (do not confuse with JSF components!) “Bean” (i.e. component) because it must satisfy JavaBeans specification requirements JavaBeans specification available at: http://www.oracle.com/technetwork/java/javase/documentation/spec- 136004.html http://www.oracle.com/technetwork/java/javase/documentation/spec- 136004.html 114 pages Introduction says: "The goal of the JavaBeans APIs is to define a software component model for Java, so that third party ISVs can create and ship Java components that can be composed together into applications by end users." JavaServer Faces7

8 Shortly about JavaBeans specification Essential JavaBean component‘s parts: Properties read/write, read-only, write-only properties, property introspection, and so on. Methods Usual public Java class methods; means for communication between JavaBean components Constructor without parameters is required (may be empty though)! JavaBean components have their own binary format JAR archives with manifest file JavaServer Faces8

9 Example of a property public class Person { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; }... } Property name is tuple {name, getName(), setName()} Without setName() we would get read-only property Without getName() we would get write-only property JavaServer Faces9

10 Bindings in JSF HTML form fields can be bound to JSF backing bean properties HTML form active components (e.g. buttons, links) can be bound to JSF backing bean methods JavaServer Faces10

11 Example of an JSF application Application consists from two HTML pages The first page allows to enter a number, and as a result outputs squared number Example of declarative validation is shown The second page shows the last calculated result Example of user session and declarative navigation JavaServer Faces11

12 A backing bean - Calculator @Named @SessionScoped public class Calculator implements Serializable { // Read/write property "number": private int number = 5; public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } // Read-only property "result": private Integer result = null; public Integer getResult() { return result; } // Method to square a number public void square() { result = number * number; } // Method to navigate to the second page public String bye() { return "enough"; } } JavaServer Faces12

13 The first HTML page – index.xhtml <html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://xmlns.jcp.org/jsf/html" xmlns:p="http://xmlns.jcp.org/jsf/passthrough"> JSF calculator JSF calculator <h:inputText id="number" value="#{calculator.number}" required="true" p:type="number" p:placeholder="Enter number"> JavaServer Faces13

14 The first HTML page – index.xhtml Result: #{calculator.result} <h:commandButton value="Square" actionListener="#{calculator.square}"/> <h:commandButton value="Bye bye" action="#{calculator.bye}" immediate="true"/> JavaServer Faces14

15 The second HTML page – bye.xhtml <html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://xmlns.jcp.org/jsf/html"> The last result Bye bye The last result: #{calculator.result} JavaServer Faces15

16 Declarative navigation: faces-config.xml /index.xhtml enough /bye.xhtml JavaServer Faces16

17 JSF request processing phases JavaServer Faces17

18 JSF request processing phases 1. Restore view – UI component tree gets restored/created on the server side 2. Apply request values – values entered to HTML form fields are being copied to JSF UI components 3. Process validations – JSF UI components validate data supplied to them 4. Update model values – converted and validated data is being copied from UI components to JSF backing beans 5. Invoke application –backing bean’s method is being invoked 6. Render response – renderer traverses through UI component tree and outputs HTML (or other markup) code JavaServer Faces18

19 Remarks actionListener – executes bound backing bean method, does not participate in navigation (application stays on the same page) action – executes bound backing bean method, participates in navigation (must return String) immediate="true" – if event is marked with this attribute, no converters and validators will be called, model will not be populated with the data from the HTML form JavaServer Faces19

20 Remarks required="true" – does not allow empty value for a form field requiredMessage –message shown if required field is left empty converterMessage – message shown if conversion is not successful (e.g. String conversion to Integer) validatorMessage – message shown if validation is not successful (e.g. number is out of valid range) JavaServer Faces20

21 Pass-through elements <html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:jsf="http://xmlns.jcp.org/jsf" xmlns:f="http://xmlns.jcp.org/jsf/core"> JSF Calculator JSF Calculator Number: <input type="number" placeholder="Enter number" required="true" jsf:id="myNumber" jsf:value="#{calculator.number}"> Result: #{calculator.result} Square Bye bye JavaServer Faces21

22 Some JSF concepts Renderer – Responsible for displaying a UI component and translating a user’s input into the component's value. Renderers can be designed to work with one or more UI components, and a UI component can be associated with many different renderers. Validator – Responsible for ensuring that the value entered by a user is acceptable. One or more validators can be associated with a single UI component. Backing beans – Specialized JavaBeans that collect values from UI components and implement event listener methods. They can also hold references to UI components. Converter – Converts a component’s value to and from a string for display. A UI component can be associated with a single converter. Messages – Information that’s displayed back to the user. Just about any part of the application (backing beans, validators, converters, and so on) can generate information or error messages that can be displayed back to the user. JavaServer Faces22

23 JavaServer Faces23 Renderer

24 RenderKit examples Browser Mobile device Telnet JavaServer Faces24

25 JSF component suites PrimeFaces (http://www.primefaces.org/)http://www.primefaces.org/ RichFaces (http://www.jboss.org/richfaces)http://www.jboss.org/richfaces ICEfaces (http://www.icefaces.org)http://www.icefaces.org OpenFaces (http://openfaces.org)http://openfaces.org TreeTable component example (PrimeFaces): JavaServer Faces25

26 What’s interesting for an architect JavaServer Faces26


Download ppt "JavaServer Faces framework. 2001 Craig McClanahan is presented that created Struts web framework and based on experience gathered designed JavaServer."

Similar presentations


Ads by Google