sessionScope HashMap&复选框

时间:2014-02-12 07:50:22

标签: javascript ajax xpages

问题

我有严重的问题(也许更多是一个理解问题?)sessionScope不想和我合作?事情就是这样:

  • 我的网站上有很多checboxes。单击一个时,ajax调用用于将checbox信息存储到sessionScope hashmap中。这很有效。
  • 如果取消选中复选框,则再次进行ajax调用,这次,它会从sessionScrope hashmap中删除该条目。这很有效。
  • 检查和取消选中作为魅力,因为我在每次ajax调用后列出hashmap以查看调试信息。

现在......如果我用复选框刷新我的主站点,另一个javascript正在使用sessionScope hashmap并检查存储在sessionScope hashmap中的复选框。非常自我解释,对吗?

好吧,在我刷新网站之后,它看起来就像页面采用PREVIOUS sessionScope HashMap(意思是那个,在我刷新页面之前)。但是,它并没有在这里结束。第二次刷新后,它会显示正确的结果。第三次刷新后,它显示以前的HashMap状态,因此它保持不变。 :|

我在这里遗漏了什么吗?一旦我刷新,HashMap现在就可以从以前的状态开始,而不需要写任何东西,只需阅读它。

这怎么可能?

请帮助我理解这件事,因为我不知道发生了什么......

最好的问候,

格里

更新(代码):

复选框页面:

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core"
    xmlns:xc="http://www.ibm.com/xsp/custom">

    <xp:this.resources>
        <xp:styleSheet href="/gama.css"></xp:styleSheet>
        <xp:styleSheet
            href="#{javascript:ROOT_PATH}/js/jquery-ui/css/smoothness/jquery-ui-1.10.3.custom.min.css">
        </xp:styleSheet>
        <xp:script src="/global.jss" clientSide="false"></xp:script>
        <xp:script src="#{javascript:ROOT_PATH}/js/jquery-2.0.3.min.js"
            clientSide="true">
        </xp:script>
        <xp:script
            src="#{javascript:ROOT_PATH}/js/jquery-ui/js/jquery-ui-1.10.3.custom.min.js"
            clientSide="true">
        </xp:script>
        <xp:script src="#{javascript:ROOT_PATH}/gama.js"
            clientSide="true">
        </xp:script>
    </xp:this.resources>


    <xp:this.data>

        <xp:dominoView var="view1" databaseName="***HIDDEN***.nsf"
            viewName="dflt">
        </xp:dominoView>
    </xp:this.data>

    <xp:table style="width:80.0%" border="1">
        <xp:tr>
            <xp:td
                style="width:100.0px;border-bottom: 1px solid #e8e8e8;" valign="top"
                styleClass="xspPanelViewColumnHeader">
                <xp:br></xp:br>
                10.5.*
            </xp:td>
            <xp:td valign="top"
                style="border-bottom: 1px solid #e8e8e8;width:auto">
                <xp:checkBox text="10.5.33" id="checkBox3"></xp:checkBox>
                <xp:checkBox text="10.5.34" id="checkBox4"></xp:checkBox>
                <xp:checkBox text="10.5.35" id="checkBox5"></xp:checkBox>
                <xp:checkBox text="10.5.38" id="checkBox6"></xp:checkBox>
                <xp:checkBox text="10.5.39" id="checkBox7"></xp:checkBox>
                <xp:checkBox text="10.5.40" id="checkBox8"></xp:checkBox>
                <xp:br></xp:br>
                <xp:checkBox text="10.5.49" id="checkBox9"></xp:checkBox>
                <xp:checkBox text="10.5.50" id="checkBox10"></xp:checkBox>
                <xp:checkBox text="10.5.51" id="checkBox11"></xp:checkBox>
                <xp:checkBox text="10.5.54" id="checkBox12"></xp:checkBox>
                <xp:checkBox text="10.5.55" id="checkBox13"></xp:checkBox>
                <xp:checkBox text="10.5.56" id="checkBox14"></xp:checkBox>
            </xp:td>
        </xp:tr>
        <xp:tr>
            <xp:td styleClass="xspPanelViewColumnHeader" valign="top"
                style="border-bottom: 1px solid #e8e8e8;">
                10.6.*
            </xp:td>
            <xp:td style="border-bottom: 1px solid #e8e8e8;">
                <xp:checkBox text="10.6.32" id="checkBox15"></xp:checkBox>

            </xp:td>
        </xp:tr>
        <xp:tr>
            <xp:td styleClass="xspPanelViewColumnHeader"
                style="border-bottom: 1px solid #e8e8e8;">
                192.*
            </xp:td>
            <xp:td style="border-bottom: 1px solid #e8e8e8;">

            </xp:td>
        </xp:tr>
        <xp:tr>
            <xp:td styleClass="xspPanelViewColumnHeader"
                style="border-bottom: 1px solid #e8e8e8;">
                193.*
            </xp:td>
            <xp:td style="border-bottom: 1px solid #e8e8e8;">

            </xp:td>
        </xp:tr>
        <xp:tr>
            <xp:td styleClass="xspPanelViewColumnHeader"
                style="border-bottom: 1px solid #e8e8e8;">
                10.21.*
            </xp:td>
            <xp:td style="border-bottom: 1px solid #e8e8e8;">


                <xp:checkBox text="10.21.8" id="checkBox2"></xp:checkBox>
                <xp:checkBox text="10.21.12" id="checkBox1"></xp:checkBox>
            </xp:td>
        </xp:tr>
        <xp:tr>
            <xp:td styleClass="xspPanelViewColumnHeader" valign="top">
                172.24.*
            </xp:td>
            <xp:td>

            </xp:td>
        </xp:tr>
    </xp:table>
    <xp:br></xp:br>


    <div id="container"></div>



    <xp:eventHandler event="onClientLoad" submit="true"
        refreshMode="norefresh">
        <xp:this.action><![CDATA[#{javascript:

        // GET INFO FROM NETKEYS SESSIONSCOPE AND SAVE IT TO JSON FORMAT
        sessionScope.netKeys = (sessionScope.netKeys || new java.util.HashMap());
        var netKeysJSON:com.ibm.commons.util.io.json.JsonJavaObject = 
          new com.ibm.commons.util.io.json.JsonJavaObject();

        var arr_key = sessionScope.netKeys.keySet();
        //print ("Printing netKeys");
        for (x in arr_key) {
            print(x +" > "+ sessionScope.netKeys[x]);
            netKeysJSON.put(x, sessionScope.netKeys[x]);
        }
        sessionScope.netKeysJSON = "";
        sessionScope.netKeysJSON = netKeysJSON.toString();


        }]]></xp:this.action>
        <xp:this.script><![CDATA[

        // READ ALL JSON ENTRIES AND CHECK SPECIFIC CHECKBOXES!
        myVar = '#{javascript:sessionScope.get("netKeysJSON")}';

        //print("Printing JSON");
        if (myVar.length > 0) {

            var arr = $.parseJSON(myVar);

            for( key in arr ) {
                console.log(key +" >> "+ arr[key]);

                //$("label[for='"+ key +"']").remove();
                $("input[id='"+ key +"']").prop('checked', true);
                loadNetsInfo(arr[key], key, true);
            }

        }

]]></xp:this.script>
    </xp:eventHandler>
</xp:view>

ajax页面:

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core" rendered="false">


    <xp:this.afterRenderResponse><![CDATA[#{javascript:
    var writer = facesContext.getResponseWriter();
    sessionScope.netKeys = (sessionScope.netKeys || new java.util.HashMap());

    var act = param.get("act");
    var ip  = param.get("ip");
    var id  = param.get("id");

    /**
     * problem!
    **/
    if (ip == null || ip.length == 0 ||
        id == null || id.length == 0 ||  
        act == null || act.length == 0) 
    {
        writer.write("ERROR: No parameters specified.");
        writer.endDocument();
        return false;
    }

    // NO PROBLEM!

    // REMOVE
    if (act == "rem") {
        sessionScope.netKeys.remove(id);
        print("Removed "+ id +" from netKeys");
    }

    // LIST ALL
    else if (act == "list") {
    print("List of all items in HashMap ("+ sessionScope.netKeys.size() +")");
        var arr_key = sessionScope.netKeys.keySet();
        for (x in arr_key) {
            print(x +" > "+ sessionScope.netKeys[x]);
            //netKeysJSON.put(x, sessionScope.netKeys[x]);
        }
    }

    // WIPE ALL
    else if (act == "wipe") {
        sessionScope.netKeys = null;
        sessionScope.remove("netKeys");
        print("Array netKeys wiped.");
    }

    // ADD + VIEW
    else if (act == "view") {
        var output = "<table style=\"display: inline-table; width: 150px;\" id=\"subnet_"+ ip +"\" border='1'><tr><td colspan=\"2\"><b>"+ ip +"</b></td></tr>";

        sessionScope.netKeys.put(id, ip);
        //writer.write("*** "+ sessionScope.netKeys.get(id));

        // start getting info now...

        var db:NotesDatabase = session.getDatabase("", "***HIDDEN***.nsf");
        var doc:NotesDocument = null;


        ////////////////
        // subnet info
        ////////////////        
        //var formula:string = "Subject=\"" + requestScope.query + "\"";
        var formula:string = "Form = \"SubnetInfo\" & subnet = \""+ ip +"\"";
        var dc:NotesDocumentCollection = db.search(formula);
        //print(formula);

        if (dc.getCount() > 0) {
            doc = dc.getFirstDocument();
            var desc = doc.getMIMEEntity("description").getContentAsText();
            output += "<tr><td colspan=\"2\">"+ desc +"</td></tr>";
        }

        else {
            print ("ID "+ id +" & IP "+ ip +" doesn't have any info. Removing entry.");
            sessionScope.netKeys.remove(id);
        }

        ////////////////
        // ip's
        ////////////////
        var formula:string = "Form = \"IP\" & Net = \""+ ip +"\"";
        dc = db.search(formula);
        //print("dc count: "+ dc.getCount());

        doc = dc.getFirstDocument();

        while (doc != null) {
            var docURL = "NetsSingle.xsp?action=editDocument&documentId=" + doc.getNoteID();
            //print(docURL);
            output += "<tr><td><a href=\""+ docURL +"\">"+ doc.getItemValueInteger("IP") +"</a></td><td>"+ ((doc.getItemValueString("inUseFor").length == 0) ? "&nbsp" : doc.getItemValueString("inUseFor")) +"</td></tr>";

            doc = dc.getNextDocument();
        }


        output += "</table>";
        writer.write(output);

    } // end: ADD + VIEW


writer.endDocument();
}]]></xp:this.afterRenderResponse>

</xp:view>

ajax函数调用(在选中/取消选中复选框时运行的javascript):

function loadNetsInfo(ip, id, add) {
    var url = "_netsGetInfo.xsp?act="+ ((add == true) ? "view" : "rem") +"&ip="+ ip +"&id="+ id;

    //alert(url);

    $.ajax({
      url: url,
      cache: false,
      success: function(result){

        if (add == true) {
            $("#container").append(result);
        }
        else {
            $("[id='subnet_"+ ip +"']").remove();
        }

    }});
}

正如@stwissel在回答我的问题时建议的那样,他就是我所做的,修复了一切。有了这个,我也优化并替换了javascript,这固定了代码过程!

faces-config.xml(通过Package Explorer打开)。请注意,我已经尝试创建自己的类(您可以扩展HashMap),但我并不需要它,所以唯一合乎逻辑且简单的方法就是添加HashMap类型的会话作用域托管bean。 / p>

  <managed-bean>
    <managed-bean-name>checkBoxStatus</managed-bean-name>
    <managed-bean-class>java.util.HashMap</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
  </managed-bean>

在我的主页面上,所有复选框现在都由托管bean处理,看起来像这样:

<xp:checkBox text="10.5.33" id="checkBox3" value="#{checkBoxStatus['10.5.33']}"></xp:checkBox>

因此值为true或false,表示是否选中。所以HashMap看起来像

checkBoxStatus['10.5.33'] = true;
checkBoxStatus['10.1.31'] = true;
checkBoxStatus['192.168.1'] = false;
checkBoxStatus['10.6.207'] = true;

上面的代码只是根据我们在faces中创建的bean来检查或取消选中复选框。

现在,我不是从会话数组创建JSON字符串,然后使用ajax调用从服务器接收信息,而是循环访问我的复选框,其中id以checkBox开头/结尾:

<xp:this.script><![CDATA[
// GO THROUGH ALL CHECKED ELEMENTS, CALL _netsGetInfo
$( "input[id*='checkBox']:checked" ).each(function( index ) {
        var id = $(this).attr('id');
        var ip = $("label[for='"+ id +"']").text();
        var ip_clean = ip.replace(/\./g, "_");

        console.log( index + ": " + id +", "+ ip +", "+ ip_clean );
        loadNetsInfo(ip, id, true);
});
]]></xp:this.script>

_netsGetInfo.xsp不像之前那样管理任何会话HashMap。

案件已关闭,一切正常作为CHARM!

非常感谢stwissel和stackoverflow社区。遗憾的是,IBM Developerworks社区根本没有帮助。

1 个答案:

答案 0 :(得分:2)

不建议在更改复选框时运行服务器往返。以下是我要做的事情:将您的复选框绑定到托管bean。 E.g称之为checkboxstatus。然后你的复选框有value =“#{checkboxstatus ['someid']}”绑定将确保值得到正确更新。 然后,您可以决定是否将每个盒子的更改发回,或者您只是将其发回给用户想要执行的操作。 bean可以是默认的HashMap