【问题标题】:Can I use csjs to clear sessionScope variables?我可以使用 csjs 清除 sessionScope 变量吗?
【发布时间】:2016-06-18 03:22:01
【问题描述】:

我有一个很好的函数,我从一些更有经验和更好的 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 成功调用吗?

【问题讨论】:

    标签: javascript xpages xpages-ssjs


    【解决方案1】:

    由于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>
    

    【讨论】:

    • 谢谢 - 我会在这周晚些时候试一试。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-22
    • 1970-01-01
    • 1970-01-01
    • 2021-07-09
    • 1970-01-01
    • 2021-08-27
    相关资源
    最近更新 更多