1

我正在尝试SelectOneListbox使用 Primefaces 在 JSF 中编写两个代码。问题是第二个 Listbox 没有将值设置为我的支持 bean 变量。不过,第一个 Listbox 工作正常。

@ManagedBean
public class AnalyseBean implements Serializable {

private static final long serialVersionUID = 1L;

private CorpusFacade corpusFacade;
private String selectedCorpus;
private String selectedDocument;
private List<Corpus> corpusList;
private List<Document> documentList;

Corpus corpus;
Document document;

public void corpusListener(AjaxBehaviorEvent e) {
    corpus = getCorpusFacade().findCorpusById(Integer.valueOf(selectedCorpus));
    documentList = corpus.getAllDocuments();
}

public void documentListener(AjaxBehaviorEvent e) {
//      document = corpus.getDocumentByName(selectedDocument);
//      System.out.println(document.getName());
}

public CorpusFacade getCorpusFacade() {
    if(corpusFacade == null) {
        corpusFacade = new CorpusFacade();
    }

    return corpusFacade;
}
public void setCorpusFacade(CorpusFacade corpusFacade) {
    this.corpusFacade = corpusFacade;
}

public String getSelectedCorpus() {
    return selectedCorpus;
}

public void setSelectedCorpus(String selectedCorpus) {
    this.selectedCorpus = selectedCorpus;
}

public List<Corpus> getCorpusList() {
    if(corpusList == null) {
        corpusList = getCorpusFacade().listAll();
    }

    return corpusList;
}

public void setCorpusList(List<Corpus> corpusList) {
    this.corpusList = corpusList;
}

public List<Document> getDocumentList() {
    if(documentList == null) {
        Corpus tempCorp = new Corpus();
        Document tempDoc = new Document("", "", "", 0.0, null, tempCorp);
        tempCorp.addDocument(tempDoc);

        documentList = tempCorp.getAllDocuments();
    }

    return documentList;
}

public void setDocumentList(List<Document> documentList) {
    this.documentList = documentList;
}

public String getSelectedDocument() {
    return selectedDocument;
}

public void setSelectedDocument(String selectedDocument) {
    this.selectedDocument = selectedDocument;
}
}   

xhtml

<h:form>
    <p:selectOneListbox id="corpList" value="#{analyseBean.selectedCorpus}">
        <f:selectItems value="#{analyseBean.corpusList}" var="corpus" itemValue="#{corpus.id}" itemLabel="#{corpus.name}" />
        <f:ajax event="change" listener="#{analyseBean.corpusListener}" render="docList" />
    </p:selectOneListbox>

    <p:selectOneListbox id="docList" value="#{analyseBean.selectedDocument}">
        <f:selectItems value="#{analyseBean.documentList}" var="document" itemValue="#{document.id}" itemLabel="#{document.name}" />
        <p:ajax event="change" listener="#{analyseBean.documentListener}" />
    </p:selectOneListbox>
</h:form>
4

0 回答 0