JSF Converter and null
cbkihong.cbkihong.hotmail.com Jul 29, 2008 4:19 AMWell, this is not strictly related to Seam I think but more related to JSF. In particular, in a Seam JSF-based application I am building, I found that converter seems not being called at all if the value expression returns null.
The details: I have an @entity with a property whose type is a java.util.Date. However, this property can be null in the database, but on rendering I would like it to be displayed - Nil -
rather than null or empty string. So I made a custom Converter that extends DateTimeConverter that returns that string when the value is null, like so:
@Converter(id="lastLogin") public class LastLoginDateConverter extends DateTimeConverter { /* (non-Javadoc) * @see javax.faces.convert.Converter#getAsString(javax.faces.context.FacesContext, javax.faces.component.UIComponent, java.lang.Object) */ @Override public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) { // TODO Auto-generated method stub if (arg2 != null && !(arg2 instanceof java.util.Date)) { throw new ConverterException("Not valid date"); } String s = super.getAsString(arg0, arg1, arg2); return (s.isEmpty() ? "- Nil -" : s); } }
and install it on the h:outputText on the JSP page where I would like it to be with a converter attribute. However, it seems like the null/empty part never get called. In fact, I tried comment-out everything in the converter and simply return 'xxxxx' and only the non-null values are affected.
I searched the Web and someone suggested the specification for JSF 1.1 seems to require implementations to bypass converter when the value is null, but I suppose Seam 2.0.x ships JSF 1.2? I checked JSF 1.2 specification and I could not find the same requirements, but still that would not explain why converter is bypassed with null values.
So, being new with JSF/Seam-based development, is making a JSF converter for presentation appropriate for this situation, or is there a better way?