selectManyMenu converter vs selectOneMenu converter

筅森魡賤 提交于 2019-12-23 01:52:16

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!