You can find the full source code for this website in the Seam package in the directory /examples/wiki. It is licensed under the LGPL.
| Online: | 18 Members of 4504 |
| Forum: Seam Users |
29. Jul 2008, 04:19 CET | Link |
Well, 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?
Yup, converters aren't run in this case.
I'll bring it up as an improvement to make to JSF 2.
Read about how to report a bug.
Hi Pete,
I can't believe that is not possible yet. It's such a shortcoming. Actually converters where called until you switched the jsf libraries in seam.
I used a converter like this to be able to type in NULL into a textfield to actually set the value to null. If I typed in an empty string, then this would be treated as such.
I noticed that in the new sun jsf libraries a converter is never run if the value of the field is null. I think that is really bad.
This converter used to work for NULLvalues with apache myfaces:
public class NullTextFieldConverter implements Converter { public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value) { if (facesContext == null) { throw new NullPointerException("facesContext"); } if (uiComponent == null) { throw new NullPointerException("uiComponent"); } return stringToValue(value); } protected Object stringToValue(String value) { if (value != null && value.equals("NULL")) return null; return value; } public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value) { // Note, this function is never called for null values! This used to work with myfaces but not with the sun libs System.out.println("getAsString " + value); if (facesContext == null) { throw new NullPointerException("facesContext"); } if (uiComponent == null) { throw new NullPointerException("uiComponent"); } return valueToString(value); } protected String valueToString(Object value) { if (value == null) { return "NULL"; } if (value instanceof String) { return (String) value; } return value.toString(); } }https://javaserverfaces-spec-public.dev.java.net/issues/show_bug.cgi?id=475
Read about how to report a bug.