我可以使用 csjs 清除 sessionScope 变量吗?

Can I use csjs to clear sessionScope variables?

我有一个很好的函数,它是我从一些更有经验和更好的 xpages 程序员那里偷来的,它使用 CSJS 清除 sessionScope:

function clearMap( map:Map ){ // Get iterator for the keys
    var iterator = map.keySet().iterator();  // Remove all items
    while( iterator.hasNext() ){  
        map.remove( iterator.next() ); 
}

是否可以修改为从 CSJS 成功调用?

由于 sessionScope 是服务器端对象,您必须使用 SSJS 代码清除它。你不能直接从 CSJS 清除它,但你可以从 CSJS 调用 SSJS 代码。要从 CSJS 调用 SSJS,您可以使用扩展库中的 JSON-RPC 服务。

这是一个例子:

<?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">
    <xe:jsonRpcService id="jsonRpcService1" serviceName="myRpcService">
        <xe:this.methods>
            <xe:remoteMethod name="clearSessionScope">
                <xe:this.script>
                    <![CDATA[
                    var iterator = sessionScope.keySet().iterator();
                    while( iterator.hasNext() ){  
                        sessionScope.remove( iterator.next() );
                    }
                    return "sessionScope cleared";
                ]]>
                </xe:this.script>
            </xe:remoteMethod>
        </xe:this.methods>
    </xe:jsonRpcService>

    <xp:button value="Clear sessionScope" id="button1">
        <xp:eventHandler event="onclick" submit="false">
            <xp:this.script>
                <![CDATA[
                var deferred = myRpcService.clearSessionScope();
                deferred.addCallback(function(result) {
                  alert(result);
                });
            ]]>
            </xp:this.script>
        </xp:eventHandler>
    </xp:button>
</xp:view>