在PrimeFaces中使用P:CommandButton

时间:2016-03-08 03:31:48

标签: jsf primefaces jsf-2.2

最初我有这个表格供用户输入并进行搜索。

         <h:form id="wordForm">
                <h:panelGrid columns="4">

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

现在我想将PrimeFaces用于自动完成功能,这是我的自动填充新表单。如何用上面的表格替换新表格?

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

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

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

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

更具体地说,我认为我仍然需要在P:CommandAction按钮中使用“action =”#{wordController.search}“,这样我就不需要在后端更改任何其他内容。但是如何通过查询参数“wordController.word”变量?因为现在“action =”#autoCompleteView.query“接受用户输入。

如何在不对当前bean代码进行重大更改的情况下修改此内容?我是否必须将原始搜索bean WordController与新的AutocompleteView bean统一?因为现在用户输入被AutoCompleteView bean接受了。

AutoCompleteView.java

@ManagedBean
public class AutoCompleteView {

    private Query query;

    @ManagedProperty("#{queryService}")
    private QueryService service;

    private List<Query> selectedQueries;

    public List<Query> completeQuery(String query) {

        System.out.println(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 void onItemSelect(SelectEvent event) {
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Item Selected", event.getObject().toString()));
    }


    public Query getQuery() {
        return query;
    }

    public void setQuery(Query query) {
        this.query = query;
    }

    public void setService(QueryService service) {
        this.service = service;
    }

    public List<Query> getSelectedQueries() {
        return selectedQueries;
    }

    public void setSelectedQueries(List<Query> selectedQueries) {
        this.selectedQueries = selectedQueries;
    }
}

根据建议编辑:

@Named
@RequestScoped
public class WordController {

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

问题:最初,我的'word'在JSF视图中通过“h:inputText”填充,而在JSF中调用search():

<h:commandButton id="search" value="Search!" action="#{wordController.search}" />

现在,如何从completeQuery()方法获取“selectedQuery”,然后使用它来填充“word”然后调用search()方法?

0 个答案:

没有答案