Monday, December 28, 2015

One of the features that is in-progress for the upcoming JSF 2.3 release is support for the Java 8 Date-Time API. This support was added to the Mojarra 2.3.0 - Milestone 4 Snapshot before JavaOne 2015. In this post, we'll take a brief look at this newly added support, which allows one to utilize the Java 8 Date-Time API with the standard JSF components via usage of the enhanced f:convertDateTime tag. We will take a look at running an example application so that you can begin testing the features of JSF 2.3 today.

In short, the f:convertDateTime tag now supports type attribute values that cover the Java 8 Date-Time data types. Namely, you can now specify: both, date, time, localDate, localTime, localDateTime, offsetTime, offsetDateTime, andzonedDateTime. You can then specify the pattern attribute on the tag to convert to the appropriate Date-Time format.

I have modified the sources for an application that I had written last year, AcmePools, such that it now supports the JSF 2.3 constructs when compiled and executed against Mojarra 2.3.0 - Milestone 4. The updated sources have been posted to a new repository for JSF 2.3 testing purposes: https://github.com/juneau001/AcmePools-JSF23. My post from 2014, Building and Testing JSF.next, has been updated to include details on utilizing the new Git repository for Mojarra. It covers the basics of building Mojarra, and testing against the snapshot. Please follow the directions in the post to get started. Next, clone the new AcmePools sources that are compatible with JSF 2.3, and open the project in NetBeans, as shown in Figure 1.

Figure 1: AcmePools-JSF23 in NetBeans

Once you've got the project open in NetBeans, you are ready to begin testing. Take a look at the /job/List.xhtml view, and look at the "Job Date" column of the dataTable. The h:outputText component has an f:convertDateTime tag applied to it, which will convert the date to the localDate type, as seen below.

Taking a look at the jobDate field of the Jobs entity, you can see that it is of type LocalDate:

@Column(name = "JOB_DATE")
private LocalDate jobDate;

Similarly, if we look at the /job/Create.xhtml view, you can see that the standard inputText component can contain an embedded f:convertDateTime tag to perform the required input conversion. In this case, we are using the PrimeFaces tag library, which also works with the f:convertDateTime tag.

Behind the scenes, this all works because of some changes made in the Mojarra javax.faces.convert.DateTimeFormatter class. JSF 2.3 requires the use of Java 8, which enables the JSF developers to directly modify the sources to include Java 8 Date-Time classes. As a quick test, I've modified the index.xhtml view of the application to include a Date-Time testing panel at the top.