0

In my servlet I am creating a multi-dimensional array that I am trying to use in my JSP to check something. Here it is my setup in the 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)  ;  

           }

I did a print of the array processed and it looks like this:

The arraylist of table used that is added to table is : [[43080, 2016, RP, 2016-02-04]]

On the JSP I am doing a loop of my table and inside the loop I check the ArrayList to see if there are processesd in servlet. Here is my JSP code:

    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") %> ) ;

         }  

In doing a debug of the screen, when I get to the alert message on the ckstatus field it fails saying that 'RP' is undefined. The first two are showing up correctly.

Is my problem that when I add to the rowpull array that I am not putting it in as a string correctly or a syntax in the alert or the actual processing that is just wrong?

Thanks again

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
hammerva
  • 129
  • 1
  • 14

2 Answers2

1

Given that the third element of your list is the string "RP", the following lines of code:

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

generate the following JavaScript code:

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

This JavaScript code thus tries displaying the value of a JavaScript variable RP. Since you haven't defined such a variable, its value is undefined.

My advice, if you really need to access data from JavaScript code, would be to

  1. Define proper classes containing fields rather than stuff everything as strings into random elements of a list
  2. Serialize these objects as JSON using a JSON marshaller and put that JSON string into a request attribute
  3. Simply have

    var arrayOfObjects = ${theJsonString};
    

    in the JSP, in order to initialize a JS variable containing the array of JavaScript objects, that you can then use in the rest of the JS code.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • All I know about JSON is what I learn in the last couple days. In looking on the web on this, it seems that JSON marshaller creates a XML file. Is that the JSON string you are talking about creating or are you talking about creating a JSON object and then using this "stringify" function? Thanks – hammerva Feb 19 '16 at 01:30
  • No, JSON and XML are unrelated formats. A JSON mrshaller, as its name indicates, transforms Java objects to a JSON structure. https://en.wikipedia.org/wiki/JSON – JB Nizet Feb 19 '16 at 06:49
0

I think I found my answer in a question from 2010. Here is what it says:

The best solution is to let JSP/JSTL generate a JavaScript object variable which you can later access in the JS code.

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

This will end up like the following in client side (rightclick page and View Source to be sure)

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

Finally rewrite checkSelection() as follows:

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

What is confusing me in reading this is what is being passed for "group" in the function. is that an index number that was done in a for loop to show groupmap[0] or something that passed a "key1" or "value1" value.

I don't have enough reputation points to ask in the thread and besides it is 6 years ago anyway. Anybody have an idea what is meant in the passing of "group"

Thanks for help.

hammerva
  • 129
  • 1
  • 14