问题
I have a converter for Album entity listed in selectOneMenu, what modifications are needed to use it with selectManyMenu if there is?
A working example of converter to use with selectManyMenu is greatly appreciated.
Converter for SelectOneMenu
package converter;
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;
import javax.faces.convert.FacesConverter;
import javax.persistence.EntityManager;
import entities.Album;
import util.EntityUtil;
@FacesConverter("albumconverter")
public class AlbumConverter implements Converter {
EntityManager em = EntityUtil.getEntityManager();
public Object getAsObject(FacesContext context, UIComponent component, String value) {
if (value == null || value.length() == 0) {
return null;
}
Album album = em.find(
Album.class,
Long.parseLong(value));
return album;
}
public String getAsString(FacesContext context, UIComponent component, Object value) {
return value instanceof Album ?
((Album) value).getAlbumId().toString() : "";
}
}
回答1:
There are no changes needed. The converter is applied on a per-item basis, not on a per-list/array basis.
来源:https://stackoverflow.com/questions/12872389/selectmanymenu-converter-vs-selectonemenu-converter