Rafał Wrzeszcz - Wrzasq.pl

ChillDev-Facelets - various Spring-based Facelets handlers

Wednesday, 12 November 2014, 22:27

Digging more and more into Java and building more and more apps with Spring I began to discover more and more technologies. One of my, probably key, decisions was to use Facelets as a view technology. Seems to be quite simple, but it isn't so - first of all Spring has a very limited integration possibilities for Facelets views; secondly Facelets are based on a different concepts than classic view templating engines like JSP, Velocity etc.; and finally Facelets are strictly designed for JSF framework so some of core features depend on it. But it's not impossible! Basic integration with Facelets is provided by Spring itself by variable resolver class that resolves Facelets variables by using Spring beans. Even better integration is possible by using Spring Web Flow which provides view resolver. What is missing is the integration of many core components, which are available in JSP tags. I began to create taglibs to fill missing gaps.


You can install the library from Maven Central Repository - it is divided into sub-packages to allow more granular dependency management:

tags related to core Spring Web and Spring WebMVC components;
Spring Security integration taglib;
Spring Social integration taglib.


Spring Web

The base taglib, pl.chilldev.facelets:facelets-taglib-spring-web, provides integration for standard Spring components. For now there are two tags - <message> which is equivalent of JSP Spring's localization tag; and <formErrors> which display binding error messages.

Note, that <formErrors> is the only form handling tag ported to Facelets, so it's not bound within form scope - when you pass a proparty path to the tag, you need to pass full path, including bean name.

<spring:formErrors path="registration.email" var="error">
    <p><spring:message message="${error}"/></p>

Spring Security

Base Facelets integration for Spring Security is provided by Spring Web Flow. But, for whatever reason, the library doesn't provide taglib definition (even though it's sample is presented in the documentation). pl.chilldev.facelets:facelets-taglib-spring-security fills this gap and also provides an extra tag tag is rather important, but missing: <authentication>.

This tag allows you to access authentication session properties from view:

<security:authentication property="principal.username" var="username"/>

Spring Social

Last (so far) taglib is pl.chilldev.facelets:facelets-taglib-spring-social, which is Spring Social integration. Basically it's just a Facelets implementation of Spring Social JSP taglib - it provides two tags: <connected>, which is a conditional element for checking if current user is connected to given provider; and <notConnected> which acts in an exactly oposite way.

<social:connected provider="facebook">
    <p>You are connected.</p>
<social:notConnected provider="facebook">
    <p><a href="${request.contextPath}/connect/facebook">Connect with Facebook.</a></p>


This is MIT-licensed open-source project published on GitHub. You don't need to ask if you can, just do all the awesome things you want!

Tags: XHTML, Web, Forms, Spring, Tutorial, ChillDev, Java, Facelets