简体   繁体   中英

Problem with Primefaces Autocomplete Component

I have a local Web Application developed with JDK 8, JSF 2.2 (implementation provided by JBoss), Spring 4.2 and Primefaces 6.2

In the application, I have one XHTML page for edit some fields of an object that is instance of a class named SiepRoEncabezado. One of those fields is an instance of SiepRpaPescador.

Until yesterday, I had one <p:selectOneMenu> contained in a form that is contained in a modal dialog in order to select one SiepRpaPescador object. The modal itself has 2 submit <p:commandButton> (one for save the changes and one for clean the form) and some aditional input fields. The modal is developed in such manner that it closes automatiquely when the data is successfully edited after I push the save button.
The modal with the <p:selectOneMenu> worked fine, but due to the fact that there are too many select items to be handled in terms of memory usage, I was forced to replace the <p:selectOneMenu> by the following component:

<p:autoComplete dropdown="true" 
                id="rpaAutoComplete"
                value="#{correccionROBean.tmpPescador}" 
                var="itemRpa"
                itemLabel="#{itemRpa.nmPescador.concat(' ').concat(itemRpa.nmPaterno).concat(' ').concat(itemRpa.nmMaterno)}"
                itemValue="#{itemRpa}"
                completeMethod="#{correccionROBean.filtrarRpa}"
                minQueryLength="4"
                maxResults="10"
                forceSelection="true" />

There, tmpPescador is a bean in the Managed Bean CorreccionROBean. The complete method there works fine and the selection items are displayed as desired. However, after I select one item in the <p:autoComplete> component and push the button to save the changes, it does nothing at all. Also, it does not display an error message and it does not throw any exception. What is worst, if I try to debug the action listener method in the button to save changes, it does nothing, it's like the action listener method is not called at all. I put an <p:ajax> tag hoping to solve the problem with no avail:

<p:autoComplete dropdown="true" 
                id="rpaAutoComplete"
                value="#{correccionROBean.tmpPescador}" 
                var="itemRpa"
                itemLabel="#{itemRpa.nmPescador.concat(' ').concat(itemRpa.nmPaterno).concat(' ').concat(itemRpa.nmMaterno)}"
                itemValue="#{itemRpa}"
                completeMethod="#{correccionROBean.filtrarRpa}"
                minQueryLength="4"
                maxResults="10"
                forceSelection="true">
    <p:ajax event="itemSelect" listener="#{correccionROBean.onRpaSelect}" update="rpaAutoComplete"/>
</p:autoComplete>

Here, when I select one item, the listener method is not triggered. When I try to debug the method, it's like the method is not called at all.

Finally, when I push the save button without selecting an item in the <p:autoComplete> , then and only then, the action listener method in the save <p:commandButton> is triggered.

What may cause this behaviour? Thanks in advance.

EDIT

I added the field immediate="true" to the autocomplete component and that triggered the ajax submit method, but still cannot execute the save button action listener method

SOLVED.

Refeer to Melloware's answer and my reply to that answer for more details.

For those that are unfamiliar with Converters:

The interface Converter, defined in JSF's API, allows to convert the data inputted and outputted to a Auto Complete component (and some other JSF UI components as well and their sub-classes). This interface has two methods:

  • public Object getAsObject(FacesContext context, UIComponent component, String value) : This method must return an Object that is instance of the same class as the value defined in the value field of the Auto Complete component (in my case, it returns a SiepRpaPescador object). You must handle exceptions in this method as it is called automatiquely in two situations:
  1. When you input characters in the Auto Complete text field and the number of inputted characters are equal or greater than the value defined in the minQueryLength field of the Auto Complete component or it's default value if not specified (in my case, when I input 4 characters or more). In this case, the value parameter in this method will be the String you inputted. Notice that if you enabled dropdown ( dropdown="true" )as in my case and you push the dropdown button, this method is NOT called.

  2. When you submit the form. In this case, the value will be the value of the labelValue field in the Auto Complete component (in my case, #{itemRpa.nrFolio} ) converted to String using the toString method defined in the class that value is instance of ( #{itemRpa.nrFolio} is an Integer , so the value will be converted using the Integer class' own implementation of toString() ) or in the Object class if no implementation of toString() is defined.

  • public String getAsString(FacesContext context, UIComponent component, Object value) : This method must return a String representation of the label value of each item to be displayed in the Auto Complete component. The parameter value is an instance of the same class as the value defined in the labelValue field of the Auto Complete component. This method is called automatiquely when the items are displayed (it doesn't matter if you inputted characters in the Auto Complete's text field or pressed the dropdown button if present) and it's called as many times as defined in the field maxResults (in my case, 10) in the Auto Complete, using the objects obtained from the returned list of the completeMethod (in my case, from each object in the list obtained from #{correccionROBean.filtrarRpa} , this method obtains it's nrFolio , as I stablished in the labelValue field of the AutoComplete).

I hope this helps to you all

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM