具有POJO和String值的Primefaces自动完成 [英] Primefaces autocomplete with POJO and String value

查看:118
本文介绍了具有POJO和String值的Primefaces自动完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要具有带有字符串值的自动完成功能,因为不能通过自动完成方法将用户限制为提供的项目,但是他们应该能够在搜索字段中写入任何内容.如果需要,他们也可以从建议中选择.

I need to have an autocomplete with string value, because users can't be restricted to provided items by autocomplete method, but they should be able to write anything in the search field. If they want, they can choose from suggestions as well.

现在我总是得到 /archive/overview.xhtml @ 28,57 itemLabel =#{item.name}":类'java.lang.String'没有属性'name'.

XHTML:

<p:autoComplete id="vyraz" value="#{archiveView.searchString}"
  completeMethod="#{archiveView.autocomplete}"
  var="item" itemLabel="#{item.name}" itemValue="#{item.name}"
  converter="archiveConverter" forceSelection="false" minQueryLength="2"
  autoHighlight="false" effect="fade">

  <p:column>
    <h:outputText value="#{item.name}"/>
    <h:outputText value=" (Barcode: #{item.barcode})" rendered="#{item.barcode ne null}"/>
  </p:column>

  <p:column>
   <h:outputText value="#{item.type.label}" style="font-weight: bold;"/>
  </p:column>
</p:autoComplete>

Bean:

private String searchString; // + getter and setter

public List<ArchiveAutoCompleteDto> autocomplete(String query) {
    // get and return from lucene index/database
}

有没有一种方法可以实现(Primefaces 5.2)?
谢谢!

Is there a way to implement this (Primefaces 5.2)?
Thanks!

推荐答案

itemValue属性可以用作转换器的轻量级替换. :autocomple小部件(基本上意味着您无法执行update="@form"或类似功能)

itemValue property in p:autocomplete can be used as a lightweight replacement of converters in just simple scenarios when you do not perform any update/refresh of the p:autocomple widget (which basically means you cannot perform update="@form" or similar)

所以基本上有3种情况:

So basically there are 3 cases:

必须为某些表达式设置属性var,才能在PrimeFaces中启用"pojo模式".

Setting the attributue var to some expression is mandatory to enable the "pojo mode" in PrimeFaces.

<p:autoComplete 
     value="#{backingBean.myPojo}"
     completeMethod="#{backingBean.autocomplete}
     var="pojo" itemLabel="#{pojo.label}"
     itemValue="#{pojo}" converter="pojoConverter">
</p:autoComplete>

在这种情况下,var="pojo"是类A的实例.value="#backingBean.myPojo}"是类型A的变量.itemValue="#{pojo}"在您请求建议列表时被求值,结果通过getAsString传递给转换器.产生要以html编码的值(例如:v1).
当您从列表中选择一个元素时(例如:v1),该元素将被传递回转换器到getAsObject中,这反过来又为您提供了要在备用Bean中设置的类型为A的对象.通常,转换器负责将Pojo转换为HTML值,反之亦然.

In this scenario var="pojo" is an instance of class A. value="#backingBean.myPojo}" is a variable of type A. itemValue="#{pojo}" is evaluated when you ask for the suggestion list, the result is passed to the converter via getAsString which produces the value to encode in html (eg.:v1).
When you select an element from the list (eg.:v1) it is passed back to the converter into getAsObject which gives you in return an object of type A to set in the backing bean. The converter as usual has full responsibility in translating from Pojo to HTML value and vice versa.

public interface Converter {

    // @return *K* the value to be used in html
    // @param obj is provided by the expression (itemValue="#{pojo}")
    public String getAsString(FacesContext context, UIComponent component, Object obj);

     // build the pojo identified by String *K*
     // @param value *K*
     public Object getAsObject(FacesContext context, UIComponent component, String value);         
}

Pojo +字符串

在这种情况下,您有一个带有String字段的pojo可以提取并在后备bean中使用.

Pojo + String

In this case you have a pojo with a String field to extract and to use in the backing bean.

<p:autoComplete value="#{backingBean.myStringValue}" 
    completeMethod="#{backingBean.autocomplete} 
    var="pojo" itemLabel="#{pojo.label}" 
    itemValue="#{pojo.stringKey}">
</p:autoComplete>

流程相同,但

  1. itemValue必须评估为String以避免ClassCasts.
  2. itemValue直接用作html值(就像它是由Converter#getAsString产生的一样),并在选定后设置为"#{backingBean.myStringValue}".
  3. "#{backingBean.myStringValue}"当然必须是字符串.
  1. itemValue must evaluate to a String to avoid ClassCasts.
  2. itemValue is used as html value directly (as if it was produced from the Converter#getAsString) and set to "#{backingBean.myStringValue}" once selected.
  3. "#{backingBean.myStringValue}" must be a string of course.

一切正常,直到您尝试执行刷新p:autoComplete小部件(例如update ="@ form")为止. Primefaces使用来自支持bean的值为String的值重新评估itemLabel(由于某种原因,它不会将itemLabel存储在ViewState中).因此,您会收到错误消息.实际上,除了提供与案例1)相同的实现之外,没有其他解决方案.

Everything works fine until you try to perform refresh the p:autoComplete widget (for example update="@form"). Primefaces re-evaluates the itemLabel(because, for some reason, it does not store the itemLabel in the ViewState) using the value from the backing bean which is a String. Therefore you get the error. Actually there is no solution to this problem but to provide an implementation as in case 1).

此处未涵盖.

这篇关于具有POJO和String值的Primefaces自动完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆