Primefaces p:数据表添加和编辑行不起作用

时间:2013-08-07 06:22:05

标签: java jsf-2 primefaces

我遇到了与Primefaces相对简单的JSF2站点的问题。我正在使用Primefaces 3.5和WebSphere Application Server v7.0

我有一个带有p:datatable的页面,它显示文档。我希望能够使用confirm / input -dialog添加,编辑和删除所述文档。但是,当我单击“添加文档”按钮时,输入文档的名称并单击“保存”,只有一个空行出现在表中。当我单击编辑按钮并给它一个正确的名称时,它会相应地保存。但是,如果我尝试编辑另一行,该对话框将显示我编辑的最后一个文档的名称。

引用可能有问题,但我无法绕过它。我试图研究这个问题,但没有用。添加(或编辑)新文档时,会调用setter两次,因此我想知道它是否与此http://code.google.com/p/primefaces/issues/detail?id=4681

有关。

这是代码文件:

test.xhtml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>test</title>
<meta http-equiv="Content-Type"
    content="application/xhtml+xml; charset=UTF-8" />
</h:head>
<h:body>
<h:form>

    <p:dataTable var="document" value="#{testMB.list}" id="documentTable"
        paginator="true" rows="15" paginatorPosition="bottom"
        paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink}"
        emptyMessage="No documents">
        <f:facet name="header">
            List of documents
        </f:facet>
        <p:column headerText="Document">
            <h:outputText value="#{document.name}" />
        </p:column>
        <p:column width="70">
            <p:commandButton icon="ui-icon-trash"
                oncomplete="deleteDocumentDlg.show()"
                style="float:right;width:32px;height:32px;top-margin:0px;bottom-margin:0px;top-padding:0px;bottom-padding:0px;">
                <f:setPropertyActionListener value="#{document}"
                    target="#{testMB.document}" />
            </p:commandButton>

            <p:commandButton icon="ui-icon-pencil"
                oncomplete="editDocumentDlg.show()"
                style="float:right;width:32px;height:32px;top-margin:0px;bottom-margin:0px;top-padding:0px;bottom-padding:0px;"
                action="#{testMB.saveChangesToDocument}">
                <f:setPropertyActionListener value="#{document}"
                    target="#{testMB.document}" />
            </p:commandButton>

        </p:column>

    </p:dataTable>

    <p:commandButton value="New document"
        oncomplete="newDocumentDlg.show()" style="float:right" />



    <!--    Add new document -dialog -->
    <p:dialog widgetVar="newDocumentDlg" id="newDocumentDialog"
        header="Add new document" message="Add new document"
        hideEffect="fade" showEffect="fade" resizable="true" closable="true"
        lazy="true" width="600">

        <h:panelGrid columns="3">
            <h:outputText value="Name" />
            <p:spacer width="10" height="10" />
            <p:inputText value="#{testMB.document.name}" width="35"
                maxlength="128" />
        </h:panelGrid>

        <br />


        <h:panelGroup layout="block" style="text-align: right">
            <p:commandButton value="Save" action="#{testMB.addDocument}"
                oncomplete="newDocumentDlg.hide();" update="documentTable">
            </p:commandButton>
            <p:commandButton value="Cancel" oncomplete="newDocumentDlg.hide();"
                action="#{testMB.cancelAction}" />
        </h:panelGroup>

    </p:dialog>



    <!--    Delete document -dialog -->
    <p:dialog widgetVar="deleteDocumentDlg" id="deleteDocumentDialog"
        header="Delete document" message="Delete document" hideEffect="fade"
        showEffect="fade" resizable="false" closable="true" lazy="true">

        <h:outputText value="Delete document?" />

        <h:panelGroup layout="block" style="text-align: right">
            <p:commandButton value="Delete" action="#{testMB.deleteDocument}"
                oncomplete="deleteDocumentDlg.hide();" update="documentTable">
            </p:commandButton>
            <p:commandButton value="Cancel"
                oncomplete="deleteDocumentDlg.hide();"
                action="#{testMB.cancelAction}" />
        </h:panelGroup>

    </p:dialog>


    <p:dialog widgetVar="editDocumentDlg" id="editDocumentDialog"
        header="Edit document" message="Edit document" hideEffect="fade"
        showEffect="fade" resizable="true" closable="true" lazy="true"
        width="600">

        <h:panelGrid columns="3">
            <h:outputText value="Name" />
            <p:spacer width="10" height="10" />
            <p:inputText value="#{testMB.document.name}" width="35"
                maxlength="128" />
        </h:panelGrid>

        <br />

        <h:panelGroup layout="block" style="text-align: right">
            <p:commandButton value="Save" action="#{testMB.saveChanges}"
                oncomplete="editDocumentDlg.hide();" update="documentTable">
            </p:commandButton>
            <p:commandButton value="Cancel" oncomplete="editDocumentDlg.hide();"
                action="#{testMB.cancelAction}" />
        </h:panelGroup>

    </p:dialog>

</h:form>
</h:body>
</html>

testMB.java

import Document

@ManagedBean
@ViewScoped
public class testMB implements Serializable {

    private Document document;

    ArrayList<Document> list;

    @PostConstruct
    public void alusta() {
        System.err.println("POSTCONSTRUCT");

        document = new Document();
        list = new ArrayList();
    }

    public String addDocument() {
        System.err.println("Adding new document: " + document.getName());

        list.add(document);
        document = new Document();

        return null;
    }

    public String deleteDocument() {
        System.err.println("Deleting document: " + document.getName());
        list.remove(document);
        document = new Document();
        return null;
    }

    public String saveChanges() {
        System.err.println("Saving changes to document: " + document.getName());
        list.get(list.indexOf(document)).setName(document.getName());
        document = new Document();
        return null;
    }

    public String cancelAction() {
        document = new Document();
        return null;
    }

    public void setDocument(Document document) {
        System.err.println("Document set to: " + document.getName());
        this.document = document;
    }

    public Document getDocument() {
        return document;
    }

    public void setList(ArrayList<Document> list) {
        this.list = list;
    }

    public ArrayList<Document> getList() {
        return list;
    }
}

Document.java

public class Document {

    private String name;

    public Document() {
        System.out.println("Document's constructor");
        name = null;
    }

    public Document(Document src) {
        System.out.println("Document's copy constructor");
        this.name = src.name;
    }

    public void setName(String name) {
        System.err.println("Name set to " + name);
        this.name = name;
    }

    public String getName() {
        return name;
    }

}

我很感激您的反馈。

1 个答案:

答案 0 :(得分:0)

您的代码存在一些问题。但我能看到的问题就在这里

<!--    Add new document -dialog -->
<p:dialog widgetVar="newDocumentDlg" id="newDocumentDialog"
    header="Add new document" message="Add new document"
    hideEffect="fade" showEffect="fade" resizable="true" closable="true"
    lazy="true" width="600">

    <h:panelGrid columns="3">
        <h:outputText value="Name" />
        <p:spacer width="10" height="10" />
        <p:inputText value="#{testMB.document.name}" width="35"
            maxlength="128" />
    </h:panelGrid>

您无法将输入文本设置为当前文档名称。

而是使用另一个变量来保存新文档名称的值,例如

private String newName;

public String getNewName() {
    return newName;
}

public void setNewName(String newName) {
    this.newName = newName;
}

然后更改你的jspx

<h:panelGrid columns="3">
            <h:outputText value="Name" />
            <p:spacer width="10" height="10" />
            <p:inputText value="#{testMB.newName}" width="35"
                maxlength="128" />
        </h:panelGrid>

然后你的addDocument()变成

    document.setName(newName);
    documentList.add(document);
    document = new Document();

我希望有所帮助。