在 JavaScript alert() 中打印 EL 字符串 “RP” 会导致 JS 错误“'RP'

Printing EL string "RP" in a JavaScript alert() causes JS error "'RP' is undefined"

本文关键字:RP JS 错误 JavaScript alert EL 打印 字符串      更新时间:2023-09-26

在我的servlet中,我正在创建一个多维数组,我试图在我的JSP中使用它来检查某些东西。 这是我在 servlet 中的设置:

        List<List<String>> tableList = new ArrayList<List<String>>();
        for (String selectedItem : selectedItems) 
           {
             String schedcombo = selectedItem;
             Integer modifyScheduleNum = Integer.parseInt(schedcombo.substring(0,5));
             Integer modifyContractYear = Integer.parseInt(schedcombo.substring(5,9));
             String newStatus = request.getParameter("ModifyStatus_" + selectedItem);
             String newStatusDate = request.getParameter("ModifyStatusDate_" + selectedItem) ;
             System.out.println("Schedule Number being processed is " + modifyScheduleNum + 
                                " with contract year of " + modifyContractYear +
                                " with modified status of " + newStatus +
                                " with modified Status Date of " + newStatusDate);
             List<String> rowpull = new ArrayList<String>();
             rowpull.add(schedcombo.substring(0,5));
             rowpull.add(schedcombo.substring(5,9)) ;
             rowpull.add(newStatus);
             rowpull.add(newStatusDate);
             tableList.add(rowpull);
             request.setAttribute("preeditList",tableList)  ;  
           }

我打印了处理后的数组,它看起来像这样:

添加到表中的表的数组列表是:[[43080, 2016, RP, 2016-02-04]]

在 JSP 上,我正在对我的表进行循环,在循环中我检查 ArrayList 以查看 servlet 中是否有进程。 这是我的 JSP 代码:

    for(var i=0; i<updateformID.selectedSched.length; i++)
           {
              var checkSchedule = document.getElementById("checkedTable").rows[i + 1].cells[1].innerHTML;
              var checkYear = document.getElementById("checkedTable").rows[i + 1].cells[2].innerHTML;
              alert("CheckSchedule being pulled outside the ForEach is " + checkSchedule) ;
             <c:remove var="cksched"/>
             <c:forEach items="${preeditList}" var="editTable">
                <c:set var="cksched" value="${editTable[0]}" scope="request" />
                alert("schedule number Inside the foreach loop " + <c:out value="${cksched}"/>) ;   
                <c:set var="ckeftyear" value="${editTable[1]}" scope="request" />
                alert("eft contract year Inside the foreach loop " + <c:out value="${ckeftyear}"/>) ;    
                <c:set var="ckstatus" value="${editTable[2]}" scope="request" />
                alert("sched status Inside the foreach loop " + <c:out value="${ckstatus}"/>) ;     
                <c:set var="ckstatusDate" value="${editTable[3]}" scope="request" />  
                <c:if test="${cksched == checkSchedule}">  
                    <c:set var="checkfound" value="YES" scope="request"/>
                </c:if>

               </c:forEach>
               alert ("The checkfound variable after the foreach is " + 
                                       <%= request.getAttribute("checkfound") %> ) ;
         }  

在对屏幕进行调试时,当我到达 ckstatus 字段上的警报消息时,它失败了,说"RP"未定义。 前两个正确显示。

我的问题是,当我添加到 rowpull 数组时,我没有正确地将其作为字符串或警报中的语法或实际处理中的语法放入错误吗?

再次感谢

鉴于列表的第三个元素是字符串"RP",以下代码行:

<c:set var="ckstatus" value="${editTable[2]}" scope="request" />
alert("sched status Inside the foreach loop " + <c:out value="${ckstatus}"/>) ;

生成以下 JavaScript 代码:

alert("sched status Inside the foreach loop " + RP) ;

因此,此 JavaScript 代码尝试显示 JavaScript 变量的值RP。由于尚未定义此类变量,因此其值为 undefined

我的建议是,如果你真的需要从JavaScript代码中访问数据,那就是

  1. 定义包含字段的适当类,而不是将所有内容作为字符串填充到列表的随机元素中
  2. 使用 JSON 封送器将这些对象序列化为 JSON,并将该 JSON 字符串放入请求属性中
  3. 只需拥有

    var arrayOfObjects = ${theJsonString};
    

    在JSP中,为了初始化一个包含JavaScript对象数组的JS变量,然后你可以在JS代码的其余部分使用它。

我想我在 2010 年的一个问题中找到了答案。 它是这样说的:

最好的解决方案是让 JSP/JSTL 生成一个 JavaScript 对象变量,稍后可以在 JS 代码中访问该变量。

     var groupMap = {
        <c:forEach items="${configuredGroupMap}" var="groupMap" varStatus="loop">
             "${groupMap.key}": "${groupMap.value}"${!loop.last ? ',' : ''}
       </c:forEach>
       };

这将在客户端中结束如下(右键单击页面并查看源代码以确保

  var groupMap = {
     "key1": "value1",
     "key2": "value2",
     "key3": "value3"
    };

最后重写 checkSelection(),如下所示:

 function checkSelection(group, tvalue) {
     if (groupMap[group] == tvalue) {
       alert("equal");
     }
 }

在阅读本文时让我感到困惑的是函数中为"组"传递的内容。 是在 for 循环中完成的索引号以显示 GroupMap[0] 或传递"key1"或"value1"值的内容。

我没有足够的声誉点可以在线程中询问,而且无论如何都是 6 年前。 任何人都知道"组"的传递意味着什么

感谢您的帮助。