JavaServer Faces in Action

1 JavaServer Faces in ActionMarty Hall, coreservlets.com ...
Author: Anissa Pierce
0 downloads 5 Views

1 JavaServer Faces in ActionMarty Hall, coreservlets.com Customized Java EE and JavaScript Training

2 Agenda Summary of evolving specs JSF 2.2 JSF 2.3Java Web Apps in Java JSF 2.2 Summary of new features One highly touted feature that really is great (with code and demo) One highly touted feature that is not as great as it sounds (with code and demo) JSF 2.3 One supposed downside that may be a blessing in disguise (with code and demo) My favorite Web stack (and why) JSF 2.3 (or 2.2), PrimeFaces, Java 8

3 Evolving Specs

4 Java SE Versions Version My Take Features Early Java versionsGood at the time. Too low level by today’s standards. Basis for Java language Java 5 Yay! Major update. Generics, varargs, printf, @Override, new “for” loop. Java 6 Yawn. Minor update. Updates to collections, Swing, etc. Java 7 Ho hum. On second thought, not bad. Medium update. Fork/join framework, diamond operator, Strings in switch statements, try-with-resources, updates to Swing (esp. new look and feel). Java 8 Yay! Yay! Major update. Lambdas for functional programming. Streams for bulk operations.

5 Web Apps in Java Version My Take Features Servlets and JSPGood at the time. Too low level by today’s standards. Servlets are foundation for most Java-based Web app frameworks. JSP now mostly obsolete. JSF 1 Boo! Years after Struts and that is all we get? Saving grace: component framework paved the way for third-party libraries like RichFaces, IceFaces, PrimeFaces, etc. JSF 2.0 Yay! Yay! Facelets instead of JSP. Annotations. Ajax support. Composite component framework. More smart defaults. Support for GET. JSF 2.1 Yawn. Small fixes and improvements. JSF 2.2 Yay! Faces flow. HTML 5 pass-through attributes. Stateless views. JSF 2.3 Yay. Several small but useful improvements. Better support for looping and tables.

6 References What’s new in JSF 2.2 What’s new in JSF 2.3 What’s new in JSF 2.3 What’s new in Java 8 Download JSF 2.2 and 2.3 https://maven.java.net/content/repositories/releases/org/glassfish/javax.faces/

7 New Features of JSF 2.2 and 2.3

8 New Features in JSF 2.2 Faces flow HTML 5 pass-through attributesPages can be grouped into chunks, each with their own beans and navigation HTML 5 pass-through attributes You can use placeholder, type="date", data-blah, etc. Stateless views No server-side, not client-side. No state saved at all. View actions Similar to normal action methods, but invoked on GET instead of POST Ajax improvements Queue control. Ajax-based file upload component. Resource library contracts Can specify prefix for folders that contain resources. Supports primitive skinning. Programmatic configuration Java calls in addition to annotations or faces-config.xml.

9 New Features in JSF 2.3 (Tentative)More options for UIRepeat and UIData Iterable, Map, custom data types Java 8 is required Java 7 code will still run (of course), but server must be running Java 8 Injection of JSF artifacts (major data structures) Simpler to access artifacts from Java instead of from EL etc. Generic types For ExternalContext#getInitParamterMap and Converter/Validator interfaces

10 My Take JSF 2.2: Faces Flow JSF 2.2: Passthrough attributes for HTML 5Highly touted feature Really great JSF 2.2: Passthrough attributes for HTML 5 Not really so great JSF 2.3: Better support for looping and data tables Seems relatively minor But very useful (and long overdue) JSF 2.3: required to use Java 8 Seen by some as an intrusive restriction Perhaps a blessing in disguise

11 JSF 2.2 – Faces Flow

12 Big Ideas Flows are groups of pages Flows use flow-scoped beansCan enter flow at only a single place (start page) Once inside, can navigate among any pages in flow Can leave flow to one or more predefined outside pages Can also have nested flows Flows use flow-scoped beans Wider scope than view scope: bean lives for same user in all pages in flow Narrower scope than session scope: when user navigates outside flow, bean is removed from session and made available for garbage collection There is also a new flowScope EL variable Note: only quick summary and example here Details and extended examples at

13 Motivation for Basic FlowsNetwork Programming: Servers Motivation for Basic Flows Modularity Pages and beans can be self contained Wizard-like structures Fits well with a common approach to many Web activities. Like a method call: A well defined entry point, list of parameters, and return value. A scope, allowing information to be available only during the invocation of the flow and to not consume resources once the flow returns. The ability to call other flows before returning Less server memory Vs. session scope, beans are removed from memory sooner: when user leaves flow Not fooled by multiple browser tabs or windows Each tab or window is considered to be a separate flow, in contrast to session scope, which is easily confused by multiple tabs or windows

14 Motivation for Nested FlowsModularity Chopping self-contained part of into separate flow makes each piece easier to understand than one large flow Reuse More than one flow can use the same nested flow For example, two different shopping flows could use the same checkout flow The calling flow just passes in the starting data Each of the shopping flows might pass in the list of items to be purchased The calling flow can get back a result The checkout flow could return a confirmation saying whether the transaction succeeded, failed, or was canceled by the user

15 Flow Scope Flow-scoped beans: bigger scope than view-scoped beansNetwork Programming: Servers Flow Scope Flow-scoped beans: bigger scope than view-scoped beans Available to the same user in all pages in a flow, rather than for a single page Flow-scoped beans: smaller scope than session-scoped beans The beans go away when user navigates out of flow There is a new EL flowScope variable That can be used to directly store simple values But, flow-scoped beans are usually better Flow-scoped beans depend on CDI They require a Java EE 7 server or CDI to be added You must

16 Simplest Usage: Flow ConventionsNetwork Programming: Servers Simplest Usage: Flow Conventions You use folder that matches flow name yourFlowName/ Folder contains empty XML file yourFlowName/yourFlowName-flow.xml Default start page matches flow name yourFlowName/yourFlowName.xhtml Other pages in flow have arbitrary names yourFlowName/anything.xhtml These are accessible only from within flow Exit (return) page uses flow name yourFlowName-return.xhtml This is not in the yourFlowName folder

17 Quick Example Using ConventionsWalk through code Folder/file structure Flow-scoped bean Forms Demo Navigation Behavior of flow-scoped bean

18 Configuration in XML: Quick SummaryBasic flows (usually in flowname-flow.xml) Define the start page startPage Define the return page(s) (required) Map outcomes to pages Nested flows One flow can invoke another Calling flow uses in the XML configuration file The nested flow returns only to calling flow The of nested flow refers to a page of the calling flow Calling flow can pass data to nested flow Calling flow stores data with Nested flow receives data with

19 Configuration in Java Setup Basic flows Nested flowsRegister a Java class and method using several standard annotations The annotated method takes a FlowBuilder Basic flows Define pages with builder.viewNode(…) Define start page with builder.viewNode(…).markAsStartNode() Define return pages with builder.returnNode(…) Nested flows Calling a nested flow and passing outbound parameters builder.flowCallNode("triggering-outcome").flowReference("", "id-of-nested-flow") .outboundParameter("paramForNestedFlow", "#{callingBean.property1}"); Receiving inbound parameters builder.inboundParameter("paramForNestedFlow", "#{nestedBean.property2}");

20 JSF 2.2 – HTML 5 Passthrough Attributes

21 Overview Idea Motivation SyntaxCan send arbitrary attributes directly to underlying HTML elements Motivation Make use of HTML 5 capabilities The placeholder attribute The data-blah attributes The new input types (type="date", type="number", type="range", type="color", etc.) Syntax Define namespace xmlns:h5="http://xmlns.jcp.org/jsf/passthrough" Use passthrough attribute

22 My Take: Yawn Placeholder text data-blah attributes New input typesRich component libraries as good or better All major libraries (PrimeFaces, RichFaces, etc.) have long supported this Some revert to JavaScript if HTML 5 placeholder attribute not supported data-blah attributes Rich component libraries do not help with this, and it is good idea But, this is not very common in JSF applications And, jQuery deals with CSS class names more easily New input types Rich component libraries much better Consistent support across browsers Can apply skinning (theming)

23 Code Walkthrough and DemoHTML 5 attributes Placeholder text Date input PrimeFaces

24 JSF 2.3 – Better Support for Looping and Data Tables

25 Overview: UIRepeat SupportThrough JSF 2.2 List Array DataModel ResultSet (boo!) null, single object Added in JSF 2.3 Iterable And thus Collection automatically Map With automatic .key and .value properties Custom type Using annotation to specify a wrapper DataModel

26 Code Walkthrough and DemoLooping With Collection (which extends Iterable) With Map

27 JSF 2.3 – Requirement to Use Java 8

28 Overview JSF 2.3 uses Java 8 internally So JSF requires Java 8Since JSF will be part of Java EE 8 So JSF requires Java 8 Even if you just use the JSF JAR Many (most?) commercial JSF applications still run on Java 7 Because the commercial server they use is not certified on Java 8 Because they don’t want to make the effort to upgrade A blessing in disguise? They should be on Java 8 now anyhow Transitioning is not as hard as they think Benefits of Java 8 are much larger than they think Hearing about JSF 2.3 now will give them nudge to move to Java 8 earlier

29 Code Walkthrough and DemoSimple String filtering in Java 8 Used in autocompleter Java 8 version simpler, more flexible, more efficient

30 My Favorite Web Stack

31 Recent Trend Simpler server environment (e.g., Spring MVC)Mostly provider of JSON Web Services Not so much a page-layout engine More explicit JavaScript on the client (e.g., jQuery and jQuery UI) More control More work

32 Job Postings

33 Google Searches

34 Job Postings

35 Google Searches

36 My Take JSF with PrimeFaces Spring MVC with jQuery Java 8Much simpler More productive Spring MVC with jQuery Better separation Front end not tied to language used on back end (and vice versa) Slightly more control on the client side (e.g., pure client-side form validation) Java 8 Big win for either approach My personal favorite JSF 2.3 PrimeFaces

37 Wrap-Up

38 Summary JSF 2.2 JSF 2.3 Java 8 My favorite comboMajor upgrade with several important new features Faces flow is one: very valuable HTML 5 passthrough attributes is another: not so valuable JSF 2.3 Relatively minor upgrade, but with some useful features and with a requirement to use Java 8 Better UIRepeat/UIData support is one: convenient and long overdue Java 8 “restriction” is not so onerous: maybe a good thing Java 8 Big win for everyone My favorite combo JSF 2.3, PrimeFaces, Java 8

39 Questions? Marty Hall, [email protected] coreservlets.comCustomized Java EE and JavaScript Training