使用sessionScope变量计算数据源-变量在部分刷新和完全刷新时被设置为null
computing data source with sessionScope variable - variable gets set to null on partial and full refresh
我试图使用sessionScope变量来存储文档id,以标识要从定义的数据源分配的文档。
在beforePageLoad中,我创建了一个文档集合并进行了简单的搜索。如果没有找到该文档,则创建一个新文档,否则将加载现有文档。一切都很好。
问题:当我对页面进行全部或部分刷新时,sessionScope变量被设置为null。尽管到处都有多个print语句,但我找不到发生这种情况的地方。许多互联网搜索并没有产生信息。下面是我的代码:
<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core" xmlns:xp_1="http://www.ibm.com/xsp/coreex">
<xp:this.beforePageLoad><![CDATA[#{javascript:sessionScope.ABBR="ASU";
sessionScope.uniSearchFormula = '@Contains(U_Abbr; "'+ sessionScope.ABBR + '")';
var checkExist:NotesDocumentCollection = database.search(sessionScope.uniSearchFormula);
var docid:NotesDocument = checkExist.getFirstDocument();
if (docid == null) {
print("Creating New Document")
var docid = database.createDocument();
docid.replaceItemValue("Form","University");
docid.replaceItemValue("U_Abbr", sessionScope.ABBR);
docid.replaceItemValue("U_Name", sessionScope.UniversityName);
docid.replaceItemValue("U_ContactName","Meerkat Watson");
docid.save();
} else {
print("Modifying Existing Document")
docid.replaceItemValue("U_ContactName","Meerkat Watson");
docid.save();
}
sessionScope.docid = docid;}]]></xp:this.beforePageLoad>
<xp:this.data>
<xp:dominoDocument var="UniversityDoc" formName="University"
action="editDocument" loaded="true" documentId="#{javascript:sessionScope.docid}">
</xp:dominoDocument>
</xp:this.data>
<xp:panel id="ButtonPanel">
<xp:div align="center">
<xp:table>
<xp:tr>
<xp:td>
<xp:button id="button3" value="Refresh Page">
<xp:eventHandler event="onclick" submit="true"
refreshMode="complete">
<xp:this.action><![CDATA[#{javascript:context.reloadPage()}]]></xp:this.action>
</xp:eventHandler>
</xp:button>
</xp:td>
<xp:td align="center">
<xp:button id="button2" value="Full Update">
<xp:eventHandler event="onclick" submit="true"
refreshMode="complete">
</xp:eventHandler>
</xp:button>
</xp:td>
<xp:td>
<xp:button id="button1" value="Partial Update">
<xp:eventHandler event="onclick" submit="true"
refreshMode="partial" id="eventHandler2" refreshId="OutputPanel">
</xp:eventHandler>
</xp:button>
</xp:td>
</xp:tr>
</xp:table>
</xp:div>
</xp:panel>
<xp:panel id="OutputPanel">
<xp:div align="center">
<xp:table>
<xp:tr>
<xp:td>
<xp:label value="sessionScope.docid = " id="label1">
</xp:label>
</xp:td>
<xp:td>
<xp:label value="#{javascript:sessionScope.docid}" id="label11">
</xp:label>
</xp:td>
</xp:tr>
<xp:tr>
<xp:td>
<xp:label id="label2" value="University Name"></xp:label>
</xp:td>
<xp:td>
<xp:text escape="true" id="computedField1" value="#{UniversityDoc.U_Name}"></xp:text>
</xp:td>
</xp:tr>
<xp:tr>
<xp:td>
<xp:label id="label3" value="University Abbr"></xp:label>
</xp:td>
<xp:td>
<xp:text escape="true" id="computedField2" value="#{UniversityDoc.U_Abbr}"></xp:text>
</xp:td>
</xp:tr>
</xp:table>
</xp:div>
</xp:panel>
</xp:view>
我办公室里一位更有经验的同事帮我思考了这个问题。我拿到的文件是这样写的:var doc:NotesDocument = checkExist.getFirstDocument();这将返回lotus.domino.local.Document的类型。
我需要使用var docid = doc.getUniversalID()获取xpage可以用来查找要返回的文档的UNID,它是typeof string。
看起来一个是指针,另一个是实际的文档,但两者的值是相同的。只是类型不同而已。这是我仍然试图缠绕我的头特别是因为一旦我设置了sessionScope。docid = docid, sessionScope。Docid返回未定义的类型.....
但它现在正在工作
这里有几个问题。您正在使用运行时计算(#{javascript:...}
),因此documentId在每次部分刷新后都会重新计算。使用${javascript:...}
没有在数据源上设置ignoreRequestParams="true"
,它从URL中拾取设置。因为URL中什么都没有,所以每次你都会得到一个新文档。
这将解决创建问题,但是您可能会遇到我在年初IBM Connect会议上讨论的问题。当附加到顶级XPage时,dominoDocument
数据源在 beforePageLoad
运行之前得到计算—它必须这样做,因为没有将组件添加到组件树以供其绑定。您将使用beforePageLoad
和documentId
属性中的print语句来确认这一点,然后查看服务器控制台。把它放在一个面板上,它就会运行。为了解决这个问题,将其放入Panel中。如果它附加到一个自定义控件,因为自定义控件(通常)被放在另一个组件中,你就可以了。或者,在表单之外设置sessionScope变量。
请参阅我幻灯片中的Twin Pines Mall/Lone Pine Mall部分http://www.slideshare.net/paulswithers1/ad1279-marty-youre-not-thinking-fourth-dimensionally-troubleshooting-xpages。恐怕您已经碰到了我在那一节中提到的大多数陷阱。
我还不确定"null"部分,但我认为你做得不对。如果你有一个像那样的绑定数据源,我可能是错的,但我认为它是在一个空白中传递的-然后它知道创建一个新文档,如果它在一个UNID中传递,然后它使用它。假设我是对的……我可能不是,但这将意味着你不需要在beforePageOpen上预先创建一个文档。只需让它为您完成这些工作,然后在保存时填充任何缺失的字段。或者如果你需要在屏幕上显示它们,只需从sessionScope中获取它们。
我不知道你在用那个搜索做什么。似乎getDocumentByKey可能会更好,因为你只是在寻找一个文档。虽然老实说,我想知道你是否会更好地通过一个参数传递你的键,并使用它。
只是一些想法。
- 如何通过ajax刷新JSF填充的javascript变量
- 在javascript中刷新页面时阻止变量的更改值
- 如何在javascript中刷新变量的内容
- 如何刷新存储和快照的 jquery 选择器变量
- Rails/JS-在不刷新页面的情况下获取变量
- 刷新页面后使用变量
- 为什么php页面刷新发布会话变量
- Node.js、Express、EJS-刷新变量以在页面上输出
- 控制器作用域变量在刷新之前未加载
- 从数组空值到浏览器刷新的 Javascript 变量
- 是否可以肯定刷新 $_SESSION[] 全局变量
- 重新初始化全局变量,而不刷新 javascript 中的页面
- 在不刷新的情况下更新标签内的变量
- 阻止默认行为后,回车键似乎正在刷新 java 脚本中的全局变量
- 尝试刷新分配给 PHP 变量的 JavaScript 变量
- Jquery 滑块自动刷新变量
- 没有刷新页面的 php/js 刷新变量
- 使用date django刷新变量
- 刷新变量javascript的值
- 传递自刷新变量