1

I'm having problems with relatively simple JSF2 site with Primefaces. I'm using Primefaces 3.5 and WebSphere Application Server v7.0

I have a page with a p:datatable which displays documents. I would like to be able to add, edit and delete said documents with a confirm/input -dialog. But when I click the Add document -button, input the name for the document and click Save, only an empty row apperas to the table. When I click the edit button and give it a proper name it is saved accordingly. But if I try to edit another row, the dialog displays the last document's name I edited.

There might be something wrong with the references, but I can't wrap my head around it. I have tried to research the subject but whit no avail. When a new document is added(or edited) the setter is called twice so I wonder if it has anything to do with this http://code.google.com/p/primefaces/issues/detail?id=4681

Here's the code-files:

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

}

I'd be grateful for your feedback.

4

1 回答 1

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();

我希望这会有所帮助。

于 2013-08-07T10:25:20.933 回答