Skip to main content

(JSF) Setting converter property in the backing PageBean:

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

Okay,

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.
arveds
Offline
Joined: 2008-12-31

I'm unfamiliar with the Woodstock components, but you may get some ideas from BalusC at http://balusc.blogspot.com/2006/06/using-datatables.html#PopulateDatatable. 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;

@ManagedBean(name="welcome")
@RequestScoped
public class Welcome {

private List data;
private HtmlDataTable dataTable;

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

public void setData(List data) {
this.data = data;
}

public String doSave() {
return null;
}

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

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

private void createTable() {
// Create .
dataTable = new HtmlDataTable();
dataTable.setValueExpression("value",
createValueExpression("#{welcome.data}", List.class));
dataTable.setVar("dataItem");

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

// Create for of 'ID' column.
HtmlOutputText idHeader = new HtmlOutputText();
idHeader.setValue("ID");
idColumn.setHeader(idHeader);

// Create for the body of 'ID' column.
HtmlInputText idInput = new HtmlInputText();
idInput.setValueExpression("value",
createValueExpression("#{dataItem.id}", Long.class));
idColumn.getChildren().add(idInput);

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

// Create for of 'Name' column.
HtmlOutputText nameHeader = new HtmlOutputText();
nameHeader.setValue("Name");
nameColumn.setHeader(nameHeader);

// Create for the body of 'Name' column.
HtmlInputText nameInput = new HtmlInputText();
nameInput.setValueExpression("value",
createValueExpression("#{dataItem.name}", String.class));
nameColumn.getChildren().add(nameInput);

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

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

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

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

entryDateColumn.getChildren().add(entryDateInput);

}

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(this.new DataType(1L, "horse"));
data.add(this.new DataType(2L, "cow", "20071214"));
data.add(this.new DataType(7L, "sheep"));
data.add(this.new DataType(13L, "pig"));
}

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

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

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

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.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");
message.setSeverity(FacesMessage.SEVERITY_ERROR);
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;
}

}