Xpages 为什么这段代码要写出两个文档?

Xpages 为什么这段代码要写出两个文档?,xpages,Xpages,有一个数据库,我只需要一个特定类别的文件。因此,当用户转到这个Xpage时,我想测试一下是否已经有一个文档,如果已经有,抓取这个文档,如果没有,那么创建并保存一个 为此,我在数据源中编写了一些ssj,但第一次运行它时,它会创建两个文档。我在代码中添加了一个打印,它会执行这部分两次。为什么会这样 <?xml version="1.0" encoding="UTF-8"?> <xp:view xmlns:xp="http://www.ibm.com/xsp/core" xm

有一个数据库,我只需要一个特定类别的文件。因此,当用户转到这个Xpage时,我想测试一下是否已经有一个文档,如果已经有,抓取这个文档,如果没有,那么创建并保存一个

为此,我在数据源中编写了一些ssj,但第一次运行它时,它会创建两个文档。我在代码中添加了一个打印,它会执行这部分两次。为什么会这样

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core"
    xmlns:xe="http://www.ibm.com/xsp/coreex">
    <xp:this.beforePageLoad><![CDATA[#{javascript:sessionScope.selectedPage = "page001"}]]></xp:this.beforePageLoad>
    <xp:this.resources>
        <xp:script src="/xpValidationDocument.jss" clientSide="false" />
        <xp:styleSheet href="/custom.css" />
    </xp:this.resources>
    <xp:this.data>
        <xp:dominoDocument var="document1" action="editDocument">
            <xp:this.documentId><![CDATA[#{javascript:sessionScope.selectedPage = "page001";
var v:NotesView = database.getView(sessionScope.selectedPage)
var doc:NotesDocuent = v.getFirstDocument()
if (doc == null)
{doc = database.createDocument();
doc.appendItemValue("form","document");
doc.appendItemValue("key",sessionScope.selectedPage);
doc.appendItemValue("crtUsr",session.getCommonUserName());
doc.appendItemValue("crtDte",session.evaluate('@Today'))
doc.save();
print ("here");
return doc.getUniversalID();}
else
{
print ("here2");
return doc.getUniversalID()}}]]></xp:this.documentId>
        </xp:dominoDocument>
    </xp:this.data>
    <xp:panel style="width:900.00px" id="pnlForm">
        <xe:widgetContainer id="widgetContainerHeader"
            style="width:100%">

            <xp:panel id="plContainer">
                <xe:formTable id="frLocationMaster"
                    disableErrorSummary="true" disableRowError="true"
                    style="lotusForm2" styleClass="scllotusui30dojo">
                    <xp:this.facets />

                    <xe:formRow id="formRow5" labelPosition="none"
                        style="padding-bottom:10.0px">
                        <xp:table style="width:99%" border="0"
                            cellpadding="0" role="presentation" cellspacing="0"
                            id="table2">
                            <xp:tr>
                                <xp:td
                                    style="width:80.00px;min-width:120px">
                                    <xp:label id="label2" for="formRow1"
                                        value="Notes" />
                                </xp:td>
                                <xp:td style="width:px">
                                    <xp:inputRichText
                                        id="inputRichText1" value="#{document1.Body}">
                                        <xp:this.attrs>
                                            <xp:attr name="toolbar">
                                                <xp:this.value><![CDATA[
            [
                ["Format", "Font", "FontSize"],
                ["Bold", "Italic", "Underline", "Strike", "-", "TextColor", "BGColor", "-", "JustifyLeft", "JustifyCenter", "JustifyRight", "JustifyBlock", "NumberedList", "-", "BulletedList"],
                ["Indent", "Outdent"],
                ["Subscript", "Superscript"],
                ["RemoveFormat", "-", "MenuPaste", "-", "Undo", "Redo", "Find", "LotusSpellChecker", "-", "Image", "Table", "Link", "Flash", "-", "PageBreak", "HorizontalRule", "SpecialChar", "Blockquote", "Smiley", "ShowBlocks"],
                ["Maximize", "Source"]
            ]
        ]]></xp:this.value>
                                            </xp:attr>
                                        </xp:this.attrs>
                                        <xp:this.dojoAttributes>
                                            <xp:dojoAttribute
                                                name="enterMode" value="2" />
                                        </xp:this.dojoAttributes>
                                    </xp:inputRichText>
                                </xp:td>
                            </xp:tr>
                        </xp:table>
                    </xe:formRow>

                </xe:formTable>
            </xp:panel>
        </xe:widgetContainer>
    </xp:panel>
</xp:view>

创建文档后,在viewScope变量中删除UNID或NoteID,并在代码顶部检查scope变量是否为null(如果不使用该变量)。 原因是加载页面时会多次重新计算数据源

所以代码应该是这样的

sessionScope.selectedPage = "page001";
if(viewScope.thisUNID==null){
var v:NotesView = database.getView(sessionScope.selectedPage)
var doc:NotesDocuent = v.getFirstDocument()
if (doc == null)
{doc = database.createDocument();
doc.appendItemValue("form","document");
doc.appendItemValue("key",sessionScope.selectedPage);
doc.appendItemValue("crtUsr",session.getCommonUserName());
doc.appendItemValue("crtDte",session.evaluate('@Today'))
doc.save();
print ("here");
viewScope.thisUNID=doc.getUniversalID()
return viewScope.thisUNID;}
else
{
print ("here2");
viewScope.thisUNID=doc.getUniversalID()
return viewScope.thisUNID}
}else{
return viewScope.ThisUNID
}

我在快速回顾中发现了几个问题

正如Said所说,documentId可能需要是页面加载绑定,而不是运行时—必须在呈现响应之前加载数据源,并且运行时绑定可能运行得不够早


但更大的问题是documentId代码不会有任何效果,因为您没有设置ignoreRequestParams=true。因此,documentId将从URL参数中提取,如果没有,它将每次创建一个新文档。

因为请求处理生命周期。尝试使用$.Paul替换documentId属性,使其在加载时进行计算,这非常有趣,因为我根据建议进行了更改,解决了两个文档的问题,但每次保存后我都会得到save/reps。返回并查看您的帖子,并将其更改为ignoreRequestParams=true,从而修复了rep/save冲突。