ReCaptcha meets JSF

PrimeFaces provides a captcha component based upon popular api reCaptcha. The component is actually an input component and has a built-in CaptchaValidator, this allows to integrate the reCaptcha well to JSF lifecycle since an invalid answer would cause a ValidatorException.

captcha1

Getting started with captcha;

1. Sign up for keys
reCaptcha is a webservice and you need two keys in order to communicate with their verify server.
You can sign up and get these keys for domain. The link is : https://admin.recaptcha.net/recaptcha/createsite/

2. Add your private key in web.xml
RecaptchaValidator needs the private key when verifying the answer, for security reasons this is not set on the components
as an attribute but as a context param in web.xml

<context-param>
<param-name>org.primefaces.ui.component.captcha.PRIVATE_KEY</param-name>
<param-value>your-private-key</param-value>
</context-param>

3. Add captcha to your JSF form
That’s it, now you can use captcha on the form you need to make sure the entity who submits the form is a human:)

<p:captcha publicKey="your-public-key"/>

4. Configuration
Multiple languages and themes are also supported, following is a Turkish captcha with different theme.

<p:captcha theme="white" language="tr" publicKey="your-public-key"/>

Online Demo
You can check out the captcha examples online.

Note that captcha component is added after 0.8.0 release and is currently in 0.8.1-SNAPSHOT of PrimeFaces as a beta component.

Flash Charts in JSF

This is the first entry of a series “PrimeFaces Diary” demonstrating the features included in PrimeFaces library. Thanks to my friend Matthias Wessendorf for suggesting the name of the series. This entry is about JSF charting and the solutions provided by PrimeFaces UI component suite.

I’ve done a lot of work about integrating charts and JSF in the past, maybe you’ve heard of “JSF ChartCreator” which uses JFreechart as the underlying engine. Although there’re various types of charts supported, chartcreator lacked interactivity since a chart is displayed as an image.
On the other hand I believe flash really fits well to data visualization requirements since they look cool and allow better user interaction..

In PrimeFaces currently there’re 6 chart components based on flash based YUI charts.

* Pie
* Line
* Column
* Bar
* Stacked Column
* Stacked Bar

Pie Chart

Let’s begin with PieChart, for a fictional data I’ll be displaying the sales of 4 brands last year. Starting point would be to create the Sale class to represent the each section in a pie.

public class Sale {

	private String brand;
	private int amount;

	public Sale() {}

	public Sale(String brand, int amount) {
		this.brand = brand;
		this.amount = amount;
	}

	public String getBrand() {
		return brand;
	}
	public void setBrand(String brand) {
		this.brand = brand;
	}

	public int getAmount() {
		return amount;
	}
	public void setAmount(int amount) {
		this.amount = amount;
	}
}

Next thing to do is to create the actual data in a JSF backing bean.

public class SalesDisplay {

	private List<Sale> sales;

	public SalesDisplay() {
		sales = new ArrayList<Sale>();
		sales.add(new Sale("Brand 1", 540));
		sales.add(new Sale("Brand 2", 325));
		sales.add(new Sale("Brand 3", 702));
		sales.add(new Sale("Brand 4", 421));
	}

	public List<Sale> getSales() {
		return sales;
	}
}

Data is ready now, so let’s add the chart to the page.

<p:pieChart value="#{salesDisplay.sales}" var="sale" categoryField="#{sale.brand}" dataField="#{sale.amount}" />

Output of this example would be;

pie1

Line Chart

Second example is about linecharts and multiple series support. For the line chart example, I’ll be using another fictional data
the number of girl-body births last years. Like the pie chart example, a Birth class is needed to represent the model.

public class Birth {

private int year;
 private int boys;
 private int girls;

public Birth() {}

public Birth(int year, int boys, int girls) {
 this.year = year;
 this.boys = boys;
 this.girls = girls;
 }

public int getYear() {
 return year;
 }

public void setYear(int year) {
 this.year = year;
 }

public int getBoys() {
 return boys;
 }

public void setBoys(int boys) {
 this.boys = boys;
 }

public int getGirls() {
 return girls;
 }

public void setGirls(int girls) {
 this.girls = girls;
 }
 }

Now the Birth class is ready, let’s create the data.

public class BirthsDisplay {

private List births;

public BirthsDisplay() {}

public BirthsDisplay() {
 births = new ArrayList();
 births.add(new Birth(2004, 120, 52));
 births.add(new Birth(2005, 100, 60));
 births.add(new Birth(2006, 44, 110));
 births.add(new Birth(2007, 150, 135));
 births.add(new Birth(2008, 125, 120));
 }

public List getBirths() {
 return births;
 }
 }

LineChart supports multiple series and each series is nested with the p:series component.

<p:lineChart value="#{chartBean.births}" var="birth" xfield="#{birth.year}">
<p:chartSeries label="Boys" value="#{birth.boys}" />
<p:chartSeries label="Girls" value="#{birth.girls}" />
</p:lineChart>

You can add as many series as you want, out of this line chart is;

line1

Extreme Makeover

It’s very likely that we need to customize the chart look and feel that our client would like:) For that there’re a couple of powerfull style attributes.
Since charts are flash based CSS styling doesn’t play well, so we’ll be using a javascript object created with the object notation. If you heard of the tv show Extreme Makeover, we’ll doing something similar here:) Our candidate is column chart displaying the births used in linechart example;

<p:columnChart value="#{chartBean.births}" var="birth" xfield="#{birth.year}">
<p:chartSeries label="Boys" value="#{birth.boys}" />
<p:chartSeries label="Girls" value="#{birth.girls}" />
</p:columnChart>

column3

Above is plain column chart, the makeover material is as follows;

var chartStyle = {
				padding : 20,
				border: {color: 0x96acb4, size: 8},
				background: {
					image : "../design/bg.jpg"
				},
				font: {name: "Arial Black", size: 14, color: 0x586b71},
				dataTip:
				{
					border: {color: 0x2e434d, size: 2},
					font: {name: "Arial Black", size: 13, color: 0x586b71}
				},
				xAxis:
				{
					color: 0x2e434d
				},
				yAxis:
				{
					color: 0x2e434d,
					majorTicks: {color: 0x2e434d, length: 4},
					minorTicks: {color: 0x2e434d, length: 2},
					majorGridLines: {size: 0}
				},
				legend: {
					display: "right"
				}
			};

			var boysSeriesStyle =
				{
					image: "../design/column.png",
					mode: "no-repeat",
					color: 0x3399FF,
					size: 35
				};

			var girlsSeriesStyle =
			{
				image: "../design/column.png",
				mode: "no-repeat",
				color: 0xFF66CC,
				size: 35
			};

Apply this style;

<p:columnChart value="#{chartBean.births}" var="birth" xfield="#{birth.year}"  style="chartStyle">
<p:chartSeries label="Boys" value="#{birth.boys}" style="boysSeriesStyle"/>
<p:chartSeries label="Girls" value="#{birth.girls}" style="girlsSeriesStyle"/>
</p:columnChart>

The good looking column chart after extreme makeover;

skinnedcolumn1

Upcoming Features
There’re two features I’m planning to add to PrimeFaces chart components soon, one is interactivity so when users
click on a series, we can be notified of these actions via ajax, second feature is polling, suppose you’ve a chart displaying the stock market
and chart data is refreshed in 10 seconds interval.

Live Demo
The examples I’ve given can be reached at the online demo PrimeFaces.

PrimeFaces reveals itself to the jedi

PrimeFaces is an open source library for Java Server Faces Web framework. Main goal of PrimeFaces is to create a component suite containing a rich set of components and provide non-rendering JSF extensions to ease development with JSF. PrimeFaces has three modules each focusing on different aspects of JSF.

UI Components

PrimeFaces UI module contains various JSF components providing rich web user experience. PrimeFaces use Yahoo UI library as the underlying client side framework. Components do all the hard work, deal with javascript rendering, and handle the JSF integration on the server side.

  • Rich set of components (HtmlEditor, ImageCropper, Dialog, AutoComplete and more).
  • Flash based Chart components.
  • Built-in Ajax with Partial Page Rendering
  • Compatible with other component libraries
  • Unobstrusive javascript rendering.
  • Well documented components.

Optimus

Optimus module provides solutions to ease the development with JSF. Optimus removes the burden of XML from JSF by providing an annotation based IOC container based powered by Google Guice and an XML-less Navigation Handler that removes need for xml-based declarative JSF navigations. Optimus also provides persistence support with JPA integration.

  • Annotation based IOC built on top of Google Guice.
  • Persistence support and JPA integration.
  • Declarative transaction management.
  • XML-Less JSF Navigations.
  • Excel and PDF export of DataTable contents.
  • Security Extensions.

FacesTrace

FacesTrace aims to enhance the traceability of JavaServer Faces based applications. Several trace information and performance metrics are collected and presented on the page being traced. FacesTrace is developed seperately before PrimeFaces is found and joined PrimeFaces project as a submodule.

  • JSF LifeCycle visualizer.
  • Performance Tracker.
  • Scoped Attributes.
  • Log4J appender.
  • FacesMessage Lister.
  • Component Tree visualizer.
Project Resources
HomePage: http://primefaces.prime.com.tr
Online Demo:  http://www.rehberharitam.com/primefaces-examples

Posted in Java. 8 Comments »

What happened to YUI4JSF

YUI4JSF was a library I came up with the idea about 1.5 years ago, although it started well, it never reached to a stable status. There were initially 5 developers and additional 5 more(never contributed though) in the team. Some time ago I’ve marked the project as stopped in sourceforge as seen here.

Although the project failed, it left a lot of experience for me behind, me and my friend Yigit are working on a new JSF project we’ll be releasing at the end of the month. New project is much more well written, documented, faster and cooler. That’s all I can say for now, arrivederci YUI4JSF.