user697911
user697911

Reputation: 10551

<p:autoComplete completeMethod> does not specify parameter in EL expression, how does it work?

In a demo I am seeing this code. In this JSF, autoCompleteView.completeQuery is called without parameter, but this method is defined with a String Parameter in the bean. Is this allowed in JSF?

<p:autoComplete id="queryPojo" value="#{autoCompleteView.query}"
 completeMethod="#{autoCompleteView.completeQuery}" var="query"
 itemLabel="#{query.displayName}" itemValue="#{query}"
 converter="queryConverter" />

Bean

public List<Query> completeQuery(String query) {

        List<Query> allQueries = service.getQueries();
        List<Query> filteredQueries = new ArrayList<Query>();

        for (int i = 0; i < allQueries.size(); i++) {
            Query skin = allQueries.get(i);
            if(skin.getName().toLowerCase().contains(query)) {
                filteredQueries.add(skin);
            }
        }         
        return filteredQueries;
    }

Edited: The search bean:

@Named
@RequestScoped
public class SearchController {

    private String word;

    // For AutoComplete suggestions
    private Query selectedQuery;

    @Inject
    private QueryService service;

    @Inject
    private Word wordObject;

    public void search() {
        if (word != null && !word.isEmpty()) {
            wordObject.searchWord(word);;
             ...
        }else {
            System.out.println("Query can't be null!");
        } 
    }

    public List<Query> completeQuery(String query) {

        List<Query> allQueries = service.getQueries();
        List<Query> filteredQueries = new ArrayList<Query>();

        for (int i = 0; i < allQueries.size(); i++) {
            Query skin = allQueries.get(i);
            if(skin.getName().toLowerCase().contains(query)) {
                filteredQueries.add(skin);
            }
        }

        return filteredQueries;
    }

    public String getWord() {
        return word;
    }

    public void setWord(String word) {
        this.word = word;
    }

   public Query getSelectedQuery() {

        return selectedQuery;
    }

    public void setSelectedQuery(Query selectedQuery) {
        this.selectedQuery = selectedQuery;
    }
}

JSF view:

<h:form>
        <p:growl id="msgs" showDetail="true" />
        <h:panelGrid columns="2" cellpadding="5">

            <p:autoComplete id="wordForm" value="#{searchController.query}"
                completeMethod="#{searchController.completeQuery}" var="query"
                itemLabel="#{query.displayName}" itemValue="#{query}"
                converter="queryConverter" forceSelection="true" />

            <p:commandButton value="search" oncomplete="PF('dlg').show()" **action="#{searchController.search}"** />

        </h:panelGrid>
    </h:form>

Since I have the p:AutoComplete form, do I still need this normal inputtext form?

                <h:inputText id="word" "
                    value="#{searchController.word}" />
                <h:message for="word" />
                <h:commandButton id="search" value="search"
                    action="#{searchController.search}" />
            </h:panelGrid>
        </h:form>

Upvotes: 0

Views: 791

Answers (1)

ScreamingTree
ScreamingTree

Reputation: 312

Yes this is allowed because the "completeMethod" automatically sets the value of the "value" tag as a Parameter inside the "completeMethod". With this approach you also can give an own domain object inside the value and work with it inside your backing bean "completeMethod" method.

Edit:

<p:autoComplete id="wordForm" value="#{searchController.word}"
completeMethod="#{searchController.completeQuery}" var="query"
itemLabel="#{query.displayName}" itemValue="#{query}"
converter="queryConverter" forceSelection="true" />

Upvotes: 1

Related Questions