在我的 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 代码访问数据,我的建议是
- 定义包含字段的适当类,而不是将所有内容作为字符串填充到列表的随机元素中
- 使用 JSON 编码器将这些对象序列化为 JSON,并将该 JSON 字符串放入请求属性中
只要有
var arrayOfObjects = ${theJsonString};
在 JSP 中,为了初始化包含 JavaScript 对象数组的 JS 变量,然后您可以在其余 JS 代码中使用该变量。
关于javascript - 在 JavaScript 警报()中打印 EL 字符串 "RP"会导致 JS 错误 "' RP' 未定义”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35483177/