JSF Workflows in a Single Page

Wizards, Conversations, Workflows in web development. Whatever you call it, it’s not trivial to deal with these properly. PrimeFaces wizard component features an AJAX powered UI and built-in partial validation to implement a conversation in a single JSF page. In the following example there’re 3 tabs corresponding to each steps of the flow and last tab is for confirmation.

wiz

This is the userWizard bean, which is simply at viewScope to span multiple requests. Session scope will work as well but optimal scope for this is the viewScope.

package org.primefaces.examples.view;

import org.primefaces.examples.domain.User;
import org.primefaces.optimus.config.Scope;
import org.primefaces.optimus.config.annotations.Controller;

@Controller(name="userWizard", scope = Scope.VIEW)
public class UserWizard {

	private User user = new User();

	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}

	public void save(ActionEvent actionEvent) {
		//Persist user
		FacesMessage msg = new FacesMessage("Successful", "Welcome :" + user.getFirstname());
		FacesContext.getCurrentInstance().addMessage(null, msg);
	}
}

And the single page that contains all steps;

<p:wizard height="200" width="600">
<p:tab>
<p:panel header="Personal Details">

	<h:messages errorClass="error"/>

	<h:panelGrid columns="2">
		<h:outputText value="Firstname: *" />
		<h:inputText value="#{userWizard.user.firstname}" required="true"/>

		<h:outputText value="Lastname: *" />
		<h:inputText value="#{userWizard.user.lastname}" required="true"/>

		<h:outputText value="Age: " />
		<h:inputText value="#{userWizard.user.age}" />
	 </h:panelGrid>
	  	</p:panel>
    </p:tab>
<p:tab>
<p:panel header="Adress Details">

	  	<h:messages errorClass="error"/>

	   		<h:panelGrid columns="2" columnClasses="label, value">
				<h:outputText value="Street: " />
				<h:inputText value="#{userWizard.user.street}" />

				<h:outputText value="Postal Code: " />
				<h:inputText value="#{userWizard.user.postalCode}" />

				<h:outputText value="City: " />
				<h:inputText value="#{userWizard.user.city}" />
	   		</h:panelGrid>
	 	</p:panel>
    </p:tab>
<p:tab>
<p:panel header="Contact Information">

	   		<h:messages errorClass="error"/>

	   		<h:panelGrid columns="2">
				<h:outputText value="Email: *" />
				<h:inputText value="#{userWizard.user.email}" required="true"/>

				<h:outputText value="Phone: " />
				<h:inputText value="#{userWizard.user.phone}"/>

				<h:outputText value="Additional Info: " />
				<h:inputText value="#{userWizard.user.info}"/>
	   		</h:panelGrid>

		</p:panel>
    </p:tab>
<p:tab>
<p:panel header="Confirmation">

	    	<h:panelGrid id="confirmation" columns="6">
				<h:outputText value="Firstname: " />
				<h:outputText value="#{userWizard.user.firstname}"/>

				<h:outputText value="Lastname: " />
				<h:outputText value="#{userWizard.user.lastname}"/>

				<h:outputText value="Age: " />
				<h:outputText value="#{userWizard.user.age}" />

				<h:outputText value="Street: " />
				<h:outputText value="#{userWizard.user.street}" />

				<h:outputText value="Postal Code: " />
				<h:outputText value="#{userWizard.user.postalCode}"/>

				<h:outputText value="City: " />
				<h:outputText value="#{userWizard.user.city}"/>

				<h:outputText value="Email: " />
				<h:outputText value="#{userWizard.user.email}" />

				<h:outputText value="Phone " />
				<h:outputText value="#{userWizard.user.phone}"/>

				<h:outputText value="Info: " />
				<h:outputText value="#{userWizard.user.info}"/>

				<h:outputText />
				<h:outputText />
			</h:panelGrid>
<p:commandButton value="Submit" actionListener="#{userWizard.save}" />

		</p:panel>
   </p:tab>

</p:wizard>

Ajax and Partial Validation

When you click next or previous, first the current tab is validated and if it’s valid next tab contents are loaded and update with ajax. If step is not valid, current tab is redisplayed with validation errors. Validation is done partially meaning only the current tab will be validated although wizard is enclosed in a single form.

Online Demos

There’re two examples of wizard deployed online;

Wizard with slide effect
Wizard with toggle effect

Posted in Developer, Java, PrimeFaces. Comments Off

Follow PrimeFaces on Twitter

PrimeFaces recently joined twitter, follow us via http://twitter.com/primefaces

Posted in Developer, Java, PrimeFaces. Comments Off

Mock OS X in a browser

From wikipedia, “Rich Internet Applications are web applications that have most of the characteristics of desktop applications.”  Well I’ve tried to go a little bit beyond these characteristics and create a real mock desktop web application. It’s called Mock OS X :) and created with PrimeFaces.

macosx

Example is built with several PrimeFaces components such as dialog, dock, growl, menubar and so on. It’s not very functional however sets a nice base example to start with. It took me 2-3 hours to create this example since everything is almost ready with PrimeFaces, I’ve just cooked it:)

If you want to check out the Mock OS X in action, visit the online demo.

Star Rating with JSF

Star based ratings are common in content driven web applications and PrimeFaces provides an out of the box solution for this. Rating component allows users to submit a rating using stars and also with Ajax RateEvent it’s possible to mark something just like in gmail where you mark an email with a star.

Basic Usage

public class Bean {

	private Double rating;

	public Double getRating() {
		return rating;
	}
	public void setRating(Double rating) {
		this.rating = rating;
	}
}
<p:rating value="#{bean.rating}" />

The output would be;

rate1

Number of Stars

By default 5 stars are displayed, this can be configured with the stars option.

<p:rating value="#{bean.rating}" stars="10"/>

rate2

Ajax RateEvent and RateListener

RateListeners are handy in case you want to listen for instant rateEvents.

<p:rating rateListener="#{bean.handleRate}" />
public class Bean {

	public void handleRate(RateEvent rateEvent) {
		Double rate = rateEvent.getRating();

		//use rate
	}
}

Online Demo

Check out the rating component in PrimeFaces Component ShowCase live demo.

Posted in Developer, Java, PrimeFaces. Comments Off

Masked Input for JSF

PrimeFaces 0.9.1 introduced a very handy component that forces an input to fit in a defined mask as input is being typed. Mask definition is generic so you can define any format you want. Following are just a couple of examples.

<h:outputText value="Date: " />
<p:inputMask value="# {maskController.date}" mask="99/99/9999"/>

<h:outputText value="Phone: " />
<p:inputMask value="# {maskController.phone}" mask="(999) 999-9999"/>

<h:outputText value="Phone with Ext: " />
<p:inputMask value="# {maskController.phoneExt}" mask="(999) 999-9999? x99999"/>

<h:outputText value="taxId: " />
<p:inputMask value="# {maskController.taxId}" mask="99-9999999"/>

<h:outputText value="SSN: " />
<p:inputMask value="# {maskController.ssn}" mask="999-99-9999"/>

<h:outputText value="Product Key: " />
<p:inputMask value="# {maskController.productKey}" mask="a*-999-a999"/>

The output of this example would be;

mask

You can try the inputMask on the prime showcase application that’s available online.

PrimeFaces UI 0.9.1 is released

PrimeFaces UI Components 0.9.1 is available to download as of today. This release introduces many new features and closes a total of 35 issues.

  • New component: Drag&Drop
  • New component: Media
  • New component: InputMask
  • New component: Dock
  • New component: OutputPanel
  • Reimplemented accordionPanel with jQuery
  • Pre and Post processors for pdf-excel data exporter

This release is highly effected by my 3 week vacation in Turkey and moving my house in London:) Despite all these we had many new features and important bug fixes reported by PrimeFaces community in our forum. Now I’m back to London and moving of house is nearly complete so I can start focus on 0.9.2. Layout components has the highest priority also new awesome tabSwitch component will be added in 0.9.2. We might also add the iCal, outlook kind of planner component but I guess it’d be in 0.9.3 not in 0.9.2.

Also don’t forget to update your PrimeFaces Reference Documentation, I’ve added 20 more pages which makes it a total of 220 pages now.