簡體   English   中英

在JSF和PrimeFaces中傳遞參數

[英]Passing parameters in JSF and PrimeFaces

我正在研究PrimeFaces AutoComplete演示。 我從完整的展示演示中簡化了它。 http://www.primefaces.org/showcase/ui/input/autoComplete.xhtml

AutoCompleteBean.java

 @ManagedBean
public class AutoCompleteBean {         
    private Query query;
    private List<Query> queries = new ArrayList<Query>();

    @PostConstruct
    public void init() {
        queries.add(new Query(0, "Afterdark", "afterdark"));
        queries.add(new Query(1, "Afternoon", "afternoon"));
        queries.add(new Query(2, "Afterwork", "afterwork"));
        queries.add(new Query(3, "Aristo", "aristo"));            
    }

    public List<Query> completeQuery(String query) { 
       List<Query> filteredQueries = new ArrayList<Query>();
        for (int i = 0; i < queries.size(); i++) {
            Query skin = queries.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;
    }
}

Query.java

public class Query {

    private int id;   
    private String displayName;  
    private String name;

    public Query() {}

    public Query(int id, String displayName, String name) {
        this.id = id;
        this.displayName = displayName;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getDisplayName() {
        return displayName;
    }

    public void setDisplayName(String displayName) {
        this.displayName = displayName;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return name;
    }
}

我省略了一個Convert類,我認為這沒有什么關系。

search.xhtml

<h:form>
    <p:growl id="msgs" showDetail="true" />
    <h:panelGrid columns="2" cellpadding="5">
        <p:autoComplete id="queryPojo" 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()"/>

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

我對此有三個問題:

1)completeMethod =“#{autoCompleteView.completeQuery}”:在不傳遞參數的情況下調用completeQuery方法,但將其定義為completeQuery(String query)。 這是如何運作的?

2)value =“#{autoCompleteView.query}”。 查詢是在AutoCompleteBean中定義的對象。 該查詢對象如何將用戶輸入的字符串作為其值? 通常,InputText的值很適合接受用戶輸入,它是一個String值。

3)我是否仍可以向p:autoComplete組件添加屬性“ action = ...”?

您在此處省略的轉換器類扮演了真正的角色。...讓我們看看您的問題

如您所見,轉換器類覆蓋了2個方法

getAsStringgetAsObject

1)價值

completeMethod="#{autoCompleteView.completeQuery}

被引用

autoCompleteView.completeQuery(autoCompleteView.query);

正如您在Query類中找到字符串方法一樣。

2).as為自動完成定義了轉換器,它調用getAsString方法在屏幕上呈現。 當選擇getAsObject方法時,將字符串值轉換為object(Query)。

3)可以使用ajax選擇事件

<p:ajax event="select" listener="#{autoCompleteView.someMethod}">

或通過p:autoComplete中的onSelect屬性調用remoteCommand

    <p:autoComplete id="queryPojo" value="#{autoCompleteView.query}" onSelect="someRemoteCommand();"
  completeMethod="#{autoCompleteView.completeQuery}" var="query"
                    itemLabel="#{query.displayName}" itemValue="#{query}"
                    converter="queryConverter" forceSelection="true" />
<p:remoteCommand name="someRemoteCommand" update="queryPojo" actionListener="#{autoCompleteView.execute}" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM