Avoid extra DB reads in the getAsObject method of converter class by caching data client side?

ぃ、小莉子 提交于 2019-11-29 07:40:27

This is indeed "by design" and perhaps a little oversight in the JSF spec. You can in theory perfectly avoid it by extracting the items from the UIComponent argument and comparing against them instead. It's however a bit of work. My colleague Arjan Tijms has written a blog about this: Automatic to-Object conversion in JSF selectOneMenu & Co.

Here's an extract of relevance; the below is the base converter which you'd need to extend instead:

public abstract class SelectItemsBaseConverter implements Converter {
    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {        
        return SelectItemsUtils.findValueByStringConversion(context, component, value, this);    
    }    
}

Here's the SelectItemsUtils class which is partly copied from Mojarra's source:

public final class SelectItemsUtils {

    private SelectItemsUtils() {}

    public static Object findValueByStringConversion(FacesContext context, UIComponent component, String value, Converter converter) {
        return findValueByStringConversion(context, component, new SelectItemsIterator(context, component), value, converter);        
    }

    private static Object findValueByStringConversion(FacesContext context, UIComponent component, Iterator<SelectItem> items, String value, Converter converter) {
        while (items.hasNext()) {
            SelectItem item = items.next();
            if (item instanceof SelectItemGroup) {
                SelectItem subitems[] = ((SelectItemGroup) item).getSelectItems();
                if (!isEmpty(subitems)) {
                    Object object = findValueByStringConversion(context, component, new ArrayIterator(subitems), value, converter);
                    if (object != null) {
                        return object;
                    }
                }
            } else if (!item.isNoSelectionOption() && value.equals(converter.getAsString(context, component, item.getValue()))) {
                return item.getValue();
            }
        }        
        return null;
    }

    public static boolean isEmpty(Object[] array) {
        return array == null || array.length == 0;    
    }

    /**
     * This class is based on Mojarra version
     */
    static class ArrayIterator implements Iterator<SelectItem> {

        public ArrayIterator(SelectItem items[]) {
            this.items = items;
        }

        private SelectItem items[];
        private int index = 0;

        public boolean hasNext() {
            return (index < items.length);
        }

        public SelectItem next() {
            try {
                return (items[index++]);
            }
            catch (IndexOutOfBoundsException e) {
                throw new NoSuchElementException();
            }
        }

        public void remove() {
            throw new UnsupportedOperationException();
        }
    }
}

Here's how you should use it for your own converter, you only have to implement getAsString() (the getAsObject() is already handled):

@FacesConverter("someEntitySelectItemsConverter")
public class SomeEntitySelectItemsConverter extends SelectItemsBaseConverter {

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        return ((SomeEntity) value).getId().toString();
    }
}

Update the above concept has ended up in JSF utility library OmniFaces in flavor of the following converters:

The only way I have found to do this so that your converter does not need to access the DB, is to make the Converter a managed bean so that it can access some other bean which stores the list of suggested values of the AutoComplete component.

Something like this:

@ManagedBean
@RequestScoped
public class EntityConverter implements Converter
{
  @ManagedProperty(value = "#{autoCompleteBean}")
  private AutoCompleteBean autoCompleteBean;

  public void setAutoCompleteBean(AutoCompleteBean autoCompleteBean)
  {
    this.autoCompleteBean = autoCompleteBean;
  }

  @Override
  public Object getAsObject(FacesContext context, UIComponent component,
        String value)
  {
    final List<Entity> entities = autoCompleteBean.getCachedSuggestions();

    for (final Enity entity : entities)
    {
      if (entity.getIdAsString().equals(value))
      {
        return entity;
      }
    }
    throw new IllegalStateException("Entity was not found!");
  }

  @Override
  public String getAsString(FacesContext context, UIComponent component,
        Object value)
  { ... }

In your jsf page, make sure you reference the converter as a bean. ie:

        <p:autoComplete value="#{autoCompleteBean.selectedEntity}"
          completeMethod="#{autoCompleteBean.getSuggestions}" var="theEntity"
          itemValue="#{theEntity}" itemLabel=#{theEntity.someValue}
          converter="#{entityConverter}">
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!