Custom JSF converter to display minutes as hh:mm

8 May 2007 at 09:40 CEST | In Features and tips, JDeveloper, Oracle |

We have a database that stores elapsed time in number of minutes in a plain number field. In our JSF/ADF Faces application we wanted to have the user input these fields as hours:minutes. So, internal storage of 210 should be displayed as “3:30″.

I knew this should be possible using a custom converter, but never looked into that. Yesterday I did and it turned out to be dead simple. Here are the few required steps:

First create a simple Java class that implements the javax.faces.convert.Converter interface:

package com.example.view.customConverters;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

import oracle.jbo.domain.Number;

public class hourMinutesConverter implements Converter {
    public hourMinutesConverter() {
    }

    public Object getAsObject(FacesContext facesContext,
                              UIComponent uiComponent,
                              String string) {
        try {
            String stringParts[] = string.split(":");
            int minutes =
                Integer.parseInt(stringParts[0]) * 60 +
                Integer.parseInt(stringParts[1]);
            Number numObj = new Number(minutes);
            return numObj;
        } catch (Exception e) {
            throw new ConverterException(e);
        }
    }

    public String getAsString(FacesContext facesContext,
                              UIComponent uiComponent,
                              Object object) {
        try {
            int totMinutes = (int)((Number)object).getValue();
            int hours = (totMinutes) / 60;
            int minutes = (totMinutes % 60);
            return hours + ":" + minutes;
        } catch (Exception e) {
            throw new ConverterException(e);
        }
    }
}

This class implements two methods: getAsObject() to convert a String (user input) to an actual Object and getAsString() to convert an actual Object to a String to display in the web page.

The error handling could be a lot better and you probably want to return meaningful error messages when the user input is not in a valid format. See the JavaDoc of javax.faces.convert.ConverterException on how to construct a ConverterException object with a simple String message or a nice javax.faces.application.FacesMessage object.

Next, go to WEB-INF/faces-config.xml and add the custom converter:


  demo.hourMinutesConverter
  
    com.example.view.customConverters.hourMinutesConverter
  

All this does is give the converter an ID which your going to use in the pages and specify the implementing class for this converter.

Finally go to the actual JSPX page and drag a Convert component from the JSF Core group in the Component Palette. Drop it on the inputText component used for your item. A dialog will appear asking your for the ID of the converter. Select “demo.hourMinutesConverter” from the list. You end up with the following code in your JSPX page:


  

I’ve misused the managerID field in a demo application and here is the result:

3 Comments

TrackBack URI

  1. Hi!

    Is it possible to add custom attributes?
    Thanks in advance!

    Comment by Flow — 1 June 2007 #

  2. Sorry Flow,

    I don’t know how to do that. I’ve tried to find an answer on the web, but couldn’t find one. If you do find one, please let me know because it’s very interesting.

    Comment by wilfred — 6 June 2007 #

  3. Great article.
    thanks!

    Comment by fetishcode — 18 June 2008 #

Sorry, the comment form is closed at this time.

Powered by WordPress with Pool theme design by Borja Fernandez.
Entries and comments feeds.