Skip to main content

(JSF) Setting converter property in the backing PageBean:

1 reply [Last post]
Joined: 2008-07-13


I am trying to create a JSF table dynamically into a JSF page, everything goes all right except for the fact that converters only de the getAsObject() but not the the getAsString

If I create the InputFields on the JSF xml page, and I set the converter attribute everything seems to go fine. But If I create the InputField in the init() or preprocessMethod of the backing PageBean the JSF Engine doesn

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2008-12-31

I'm unfamiliar with the Woodstock components, but you may get some ideas from BalusC at This uses straight JSF Core/HTML components, but I figure you'd be able to adapt to Visual Web JSF in NetBeans.

To be honest, if I had to create a component tree in the backing bean (I never have, and probably never will) I think I'd create it from scratch.

I've included modified code at the end of this post, which works - I have a 4 row table, and I get 4 calls to getAsString() or getAsObject() when expected. This code is an adaptation of the code presented in the link above (*), but in fact it's fairly standard stuff.

HTH, Arved

* BalusC has a fine website for JSF stuff. He just asks that his site be mentioned if code is used and adapted from there.

********** Relevant piece from web page **********

********** backing bean (modify to JSF 1.x as required) **********

package org.ahs.main;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.el.ValueExpression;
import javax.faces.component.html.HtmlColumn;
import javax.faces.component.html.HtmlDataTable;
import javax.faces.component.html.HtmlInputText;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.context.FacesContext;
import javax.faces.model.ManagedBean;
import javax.faces.model.RequestScoped;
import org.ahs.jsf.MyDateTimeConverter;

public class Welcome {

private List data;
private HtmlDataTable dataTable;

public List getData() {
if (data == null) {
return data;

public void setData(List data) { = data;

public String doSave() {
return null;

public HtmlDataTable getDataTable() {
if (dataTable == null) {
return dataTable;

public void setDataTable(HtmlDataTable dataTable) {
this.dataTable = dataTable;

private void createTable() {
// Create .
dataTable = new HtmlDataTable();
createValueExpression("#{}", List.class));

// Create for 'ID' column.
HtmlColumn idColumn = new HtmlColumn();

// Create for of 'ID' column.
HtmlOutputText idHeader = new HtmlOutputText();

// Create for the body of 'ID' column.
HtmlInputText idInput = new HtmlInputText();
createValueExpression("#{}", Long.class));

// Create for 'Name' column.
HtmlColumn nameColumn = new HtmlColumn();

// Create for of 'Name' column.
HtmlOutputText nameHeader = new HtmlOutputText();

// Create for the body of 'Name' column.
HtmlInputText nameInput = new HtmlInputText();
createValueExpression("#{}", String.class));

// Create for 'Entry Date' column.
HtmlColumn entryDateColumn = new HtmlColumn();

// Create for of 'Entry Date' column.
HtmlOutputText entryDateHeader = new HtmlOutputText();
entryDateHeader.setValue("Entry Date");

// Create for the body of 'Entry Date' column.
HtmlInputText entryDateInput = new HtmlInputText();
createValueExpression("#{dataItem.entryDate}", Date.class));

// add f:converter
MyDateTimeConverter mdtc = new MyDateTimeConverter();



private ValueExpression createValueExpression(String valueExpression, Class valueType) {
FacesContext facesContext = FacesContext.getCurrentInstance();
return facesContext.getApplication().getExpressionFactory().createValueExpression(
facesContext.getELContext(), valueExpression, valueType);

private void populateData() {
data = new ArrayList();
data.add( DataType(1L, "horse"));
data.add( DataType(2L, "cow", "20071214"));
data.add( DataType(7L, "sheep"));
data.add( DataType(13L, "pig"));

public class DataType {
private Long id;
private String name;
private Date entryDate;

public DataType(Long id, String name) { = id; = name;
this.entryDate = Calendar.getInstance().getTime();

public DataType(Long id, String name, String dateStr) { = id; = name;
try {
this.entryDate = new SimpleDateFormat("yyyyMMdd").parse(dateStr);
} catch (ParseException parseException) {

public Long getId() {
return id;

public void setId(Long id) { = id;

public String getName() {
return name;

public void setName(String name) { = name;

public Date getEntryDate() {
return entryDate;

public void setEntryDate(Date entryDate) {
this.entryDate = entryDate;

********** The converter (which basically is a rudimentary f:convertDateTime) **********

package org.ahs.jsf;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;

public class MyDateTimeConverter implements Converter {

public Object getAsObject(FacesContext arg0, UIComponent arg1, String dateStr) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

Date result = null;
try {
result = sdf.parse(dateStr);
} catch (ParseException ex) {
Logger.getLogger(MyDateTimeConverter.class.getName()).log(Level.SEVERE, null, ex);
FacesMessage message = new FacesMessage();
message.setDetail("Date not valid: format 'yyyyMMdd' expected");
message.setSummary("Date not valid: format 'yyyyMMdd' expected");
throw new ConverterException(message);
return result;

public String getAsString(FacesContext arg0, UIComponent arg1, Object value) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

Date date = (Date)value;
String result = null;

result = sdf.format(date);

return result;