<p:autocomplete completemethod =“”>没有在EL表达式中指定参数,它是如何工作的?

时间:2016-03-09 07:23:15

标签: jsf jsf-2 primefaces el methodexpression

在演示中我看到了这段代码。在这个JSF中,autoCompleteView.completeQuery在没有参数的情况下被调用,但是这个方法是在bean中使用String Parameter定义的。这是JSF允许的吗?

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

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;
    }

编辑:搜索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视图:

<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>

由于我有p:AutoComplete表单,我还需要这个普通的inputtext表单吗?

                

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

1 个答案:

答案 0 :(得分:1)

是的,这是允许的,因为“completeMethod”会自动将“value”标记的值设置为“completeMethod”中的Parameter。 使用这种方法,您还可以在值中提供自己的域对象,并在支持bean“completeMethod”方法中使用它。

编辑:

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