I want to avoid the boiler plate code for creating a SelectItems list to display my / dtos objects between the view and the model, so I used this fragment of a generic converter object:
@FacesConverter(value = "objectConverter") public class ObjectConverter implements Converter { private static Map<Object, String> entities = new WeakHashMap<Object, String>(); @Override public String getAsString(FacesContext context, UIComponent component, Object entity) { synchronized (entities) { if (!entities.containsKey(entity)) { String uuid = UUID.randomUUID().toString(); entities.put(entity, uuid); return uuid; } else { return entities.get(entity); } } } @Override public Object getAsObject(FacesContext context, UIComponent component, String uuid) { for (Entry<Object, String> entry : entities.entrySet()) { if (entry.getValue().equals(uuid)) { return entry.getKey(); } } return null; } }
Already there are answers to similiar questions, but I want a vanilla solution (without * faces). The following points still leave me uncertain about the quality of my fragment:
- If it was that simple, why is there no built-in object converter in JSF ?
- Why are so many people still using SelectItems ? Isn't there more flexibility using a common approach? For instance. # {dto.label} can be quickly changed to # {dto.otherLabel}.
- Given that a region is simply a mapping between a view and a model, is there any significant drawback to the general approach?
source share