Posts

Showing posts from January, 2012

CDI based @Asynchronous alternative

Arguably one of the most convenient things in EJB after declarative transactions is the @Asynchronous annotation . Applying this annotation to a method will cause it to be executed asynchronously when called (the caller does not have to wait for the method to finish executing). The downside of this annotation is that it's only applicable to EJB beans. While EJB beans these days are lightweight and nothing to avoid in general, the fact is that in Java EE 6 and especially Java EE 7 other managed beans, specifically CDI ones, play an increasingly important role. These beans unfortunately can not directly take advantage of the platform provided @Asynchronous . Building such support ourselves in Java EE 7 however is not that difficult. Thanks to the Java 8, and the Interceptors and Concurrency specs it's actually quite simple, but with a small caveat (see below) : We'll start with defining the annotation itself: @InterceptorBinding @Target({METHOD}) @Retention(RUNTIM

Easily disable sorting in PrimeFaces 3's DataTable

PrimeFaces provides a convenient and easy to use sorting facility for its DataTable . Together with Facelets, this facility allows us to create re-usable columns that are natural sortable by default. E.g.: <ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:p="http://primefaces.org/ui" > <p:column headerText="#{headerText}" sortBy="#{value}"> <h:outputText id="#{id}" value="#{value}" /> </p:column> </ui:composition> Such a column can be used on a page inside a DataTable as follows: <my:sortableColumn id="foo" value="#{someBean.someValue}" /> The problem The above is only a simple example, and real-life usage can be more complex with e.g. default styles and cell editing capabilities added. With such complex tags, i

Passing action methods into Facelets tags

The problem JSF, via Facelets, has various mechanisms to easily reuse view content. One of those is the Facelets tag, which allows one to reuse markup and/or components . One notorious problem with these Facelets tags is that you can't directly pass action methods (method expressions) into them. There's a workaround where you break the expression into two parts, the base (which should be a value expression) and the name of the method as a plain string. Inside the tag these two are then combined again, e.g. via the array notation syntax. See e.g. Passing action methods in Facelets using array notation Facelets Param for action method Another approach is to create a small helper tag that converts the value expression in some way into a method expression. There have been a couple of implementations of this idea: Creating composite controls with JSF and facelets How the button is created and how it acts (see ActionMapperTagHandler at the bottom