days
-1
-4
hours
-1
0
minutes
-1
-8
seconds
0
0
search
As explained by Java Champion Josh Juneau

Looking at the brand new JSF 2.3: New features for an old favorite

Josh Juneau
JSF
Latte coffee image via Shutterstock.

JSF has been around forever but it’s still near and dear to our hearts. In this article, Josh Juneau explains some of the newest features of this old workhorse, including the new API enhancements, CDI alignment improvements, and even some timely updates.

JavaServer Faces (JSF) has been one of the most widely used frameworks for developing web applications on the Java EE Platform.  Introduced in 2002, this framework provided a different approach to constructing web applications by allowing developers to construct web views using components, rather than building everything from scratch.  Components allow developers to focus more on the business logic of an application, and spend less time on developing page actions and styling since JavaScript and CSS are abstracted away.  JSF also provided a different approach to working with application state, as state management was handled by the framework.

Over the years, JSF has been enhanced to help make development with the framework more productive and also maintain relevance with the introduction of newer web technologies.  Facelets were made a core part of the framework, allowing developers to create templates that could easily be applied to all views within an application.  Flows were introduced, providing a means to carry state across a number specified views for more detailed session management.  Changes have been made to allow components and views incorporate better client and server side interoperability via AJAX.  These are just a few of the improvements that have helped to refine the JSF framework into what has become one of the top web frameworks today.

The most recent iteration of JSF is version 2.3, which includes a number of more improvements and refinements, bringing it up to speed with Java 8 and other modern web frameworks in use today.  Since there are a number of resources that explain how to utilize JSF, I will cover the latest features that are included in JSF 2.3 release in this article.  The focus for this release is for better alignment with Java SE 8 and CDI, and a small new feature set based upon popular community requests.  In this article, we will go through a handful of the new features and enhancements.

Improved CDI alignment and ease of use

Perhaps one of the most sought after and useful additions to JSF 2.3 is the CDI alignment improvements.  JSF 2.3 offers a number of productivity enhancements as many JSF artifacts can now be easily injected into Java classes and EL expressions.  For years, a JSF pain point has been the requirement to utilize static entry methods and chaining to obtain a number of artifacts, such as FacesContext, RequestMap, or FlowMap.  These artifacts can now easily be injected into classes, rather than instantiated.  We used to do the following:

FacesContext facesContext = FacesContext.getCurrentInstance(); 

In JSF 2.3, we can simply do the following:

@Inject
FacesContext facesContext;

If using within the context of an EL expression within a JSF view, one could do the following:

#{facesContext}

There are a number of artifacts that do require a CDI qualifier.  For instance, FlowMap can be injected as follows:

@Inject
@FlowMap
private Map<Object, Object> flowMap;

One of the useful features of JSF is the easy development of converters, validators, and behaviors.  In JSF 2.3, it is now possible to inject CDI artifacts into these targets, making them even easier to create.  JSF Managed Bean scoping has been a bit confusing over the past few years because it has been possible to utilize both Managed Bean annotations as well as CDI for scoping.  With the release of JSF 2.3, Managed Bean annotations are deprecated, as CDI is now the preferred approach.  This should help prevent developers from introducing errors when mixing Managed Bean and CDI scope annotations, as they do not play well together.  As such, the @ManagedProperty annotation is being deprecated.  An updated @ManagedProperty annotation is introduced for this release, allowing the same functionality to remain available.

@Inject
@ManagedProperty (“#{bean.property}”)
private String stringProperty; 

Networking and WebSockets updates

A WebSocket is a protocol that provides full duplex bidirectional communication over TCP.  It has become a standard communication protocol in this ever-growing asynchronous landscape.  Although JSF has worked well with WebSocket communication for quite some time now, the 2.3 release fully embraces it, making WebSockets a first class citizen via the new <f:websocket> tag.  The new tag can be placed into a view to allow server-side communications to be pushed to all instances of a socket containing the same channel name.  When the communication is received, an onmessage JavaScript event handler can be invoked, providing client side functionality.  An example using the new tag may look like the following, which simply displays an alert when the message is received:

<h:body> 
    <f:websocket channel=“jaxArticle” onmessage=“function(message)
{alert(message)}” /> 
</h:body> 

The server side of a WebSocket communication has the ability to push out messages.  JSF 2.3 adds the javax.faces.push.PushContext, which is an injectable context, allowing server push to a named channel.

There are a number of other networking and AJAX enhancements in JSF 2.3, including the following:

  • Execute arbitrary server-side methods from a view with the new <h:commandScript/> component.
  • Execute JavaScript within a view from a server-side method via PartialViewContext.
  • Update multiple forms using AJAX via the newly introduced “Namespace” mode.

Validation and conversion enhancements

There have been a number of useful validation and conversion enhancements made for this release, including better Java SE 8 alignment and the ability to validate an entire bean. Class-level bean validation has been made possible via the use of the new <f:validateWholeBean> tag. By including this tag within a view and marking at least one input component with a bean validation group, the entire bean can be validated, enabling new validation support such as cross-field validation.

Since Java SE 8 has been released, much of the Java world has been utilizing the new Date-Time API. The Java EE world has had to use work arounds to make this new API work, and the JSF 2.3 release moves forward, enhancing the <f:convertDateTime> conversion tag to support the new Date-Time types. For example, to utilize the java.time.LocalDate class, one can use the following within the view:

<h:outputText value="#{jobController.selected.workDate}">
  <f:convertDateTime type="localDate" pattern="MM/dd/yyyy"/>
</h:outputText>

The <f:convertDateTime> has been enhanced to support the following Date-Time types: LocalDate, LocalTime, LocalDateTime, OffsetTime, OffsetDateTime, and ZonedDateTime.

Component and API enhancements

Since the inception of JSF, there have been a number of nuances across the various components and APIs which sometimes makes development cumbersome.  In an effort to ease some of these pain points, there have been enhancements to a number the underlying APIs and components.  For instance, UIData and UIRepeat have been enhanced to support the Map and Iterable interfaces, as well as custom types.  These enhancements make it possible to support java.util.Iterable types, Maps, and custom data types in DataTables and with the ui:repeat tag.  For example, map support in a DataTable looks like the following code:

<h:dataTable var="mapEntry" value="#{jobController.jobMap}">
  <h:column value="#{mapEntry.key}"/>
  <h:column value="#{mapEntry.value}"/>
</h:dataTable>

There are a number of other useful enhancements including the ability to utilize constants within EL via the new tag.  For years now JSF has been missing a few basic features such as the ability to place a radio button anywhere in a view.  The 2.3 release decreases the number of sought after features by providing the following:

  • Standalone radio button via the new group attribute on <h:selectOneRadio>

  • Added styleClass attribute on <h:column> for enhanced style support

  • Added rowClass attribute on <h:dataTable> for enhanced style support

  • Automatic Collection Conversion for the <h:selectManyMenu> tag

Conclusion

The JSF 2.3 release provides many enhancements and new features which make the framework easier and more flexible.   Oracle and the community came together to make this release even bigger than originally planned.  Many of the features that have been included in this release have been requested by the community, and there was a great deal of work completed by the community members of the JSR 372 expert group.

This article touches upon a few of the new features and enhancements, but there are a number more that weren’t mentioned.  For a more complete list, please visit Arjan Tjims Weblog, which provides a great summary of the features.  The JSF 2.3 release will help to make the framework viable well into future.

Author
JSF

Josh Juneau

Josh Juneau is a Java Application Developer in the suburbs of Chicago, IL USA.  He has focused on Java EE and database driven applications since the early 2000s.  Josh has authored a number of books pertaining to the Java Platform, and he frequently writes articles for Oracle Java Magazine and OTN. He is a member of the JCP and served on both the JSR-372 (JSF 2.3), and JSR-378 (Portlet Bridge for JSF 2.2) Expert Groups.  He is a Java community advocate and contributor, helping to lead the Chicago JUG Adopt-a-JSR effort.  He is a Java Champion and a member of the NetBeans Dream Team, as well as the Java EE Guardians.


Leave a Reply

1 Comment on "Looking at the brand new JSF 2.3: New features for an old favorite"

avatar
400
  Subscribe  
Notify of
wklaczynski
Guest

I suggest a solution with “State Chart Flow Faces” navigation. He solves many problems.
https://github.com/wklaczynski/state-flow-faces