Solved

JSP for loop issue during creation of JavaScript Object in JSP page

Posted on 2008-10-14
3
891 Views
Last Modified: 2008-11-05
I created  an Object list based on below class and send that over to JSP page  
List<list> account = ArrayList<String> ()         <----- list of account

List<?> myList = new Client( clientId , account  )     <-----   Object that will be sent to JSP


class Client {
    String clientId;
    List<String> accountList;

}

Below is the list content that I iterate before sending to JSP page  to make sure the data in a list are correct:

                  clinetID        account          
Index[0] = 15E5Y8------15E5Y8A
Index[1] = 15E6L8------No Account.
Index[2] = 15E882------15E882A
Index[3] = 15E8L8------15E8L8A
Index[4] = 15E8L8------15E8L8B
Index[5] = 15E8R6------No Account.
Index[6] = 15E9M9------15E9M9A

in order to create the Javascript Object I have made below Script

<script type="text/javascript">
      var clientObjectList = new Array();        
      function Client(clientId,accountId) {
                this.clientId=clientId;
                this.accountId = accountId;
      }  

<c:forEach var="view" items="${sessionScope.keyValueResult.key }"  varStatus="status">      
  <c:set var="listAccount" value="${view.accountList}" />
  <c:forEach var="viewAccount" items="${listAccount}" varStatus="listStatus">
   clientObjectList[${status.count - 2 + listStatus.count}] = new Client("${view.clientId}","${viewAccount}");
</c:forEach>
</c:forEach>  
</scrip>


below is my JavaScript Object list after running the JSP page

      clientObjectList[0] = new client("15E5Y8" , "15E5Y8A");

      clientObjectList[1] = new client("15E6L8" , "No Account in .")

      clientObjectList[2] = new client("15E882" , "15E882A");

      clientObjectList[3] = new client("15E8L8" , "15E8L8A");

      clientObjectList[4] = new client("15E8L8" , "15E8L8B");       

       the next one should be 5 but it's 4
      clientObjectList[4] = new client("15E8R6" , "No Account .");      

      clientObjectList[5] = new client("15E9M9","15E9M9B");            

Please look at the next index after  clientObjectList[4]   --> Must be 5 but it's getting 4 also

Problem :  it seems  when the list has a size more than 1  the index or var="status.count" is  not increased  when iteration is done in nested FOR <c:foreach var="viewAccount")

I did couple different way of programming syntax but the result were the same, something ike below  

<c:set var="index" value="0" />
<c:forEach var="view" items="${sessionScope.keyValueResult.key }"  varStatus="status">      
<c:forEach var="viewAccount" items="${listAccount}" varStatus="listStatus">            
clientObjectList[${index}] = new Client("${view.clientId}","${viewAccount}");
<c:set var="index" value="${index + 1}" />
</c:forEach>
</c:forEach>  

Does anybody have any Ideas ? Thanks
0
Comment
Question by:arkhls
  • 2
3 Comments
 
LVL 17

Expert Comment

by:sweetfa2
ID: 22717633
  clientObjectList[${status.count - 2 + listStatus.count}] = new Client("${view.clientId}","${viewAccount}");

maybe should be:

   clientObjectList[${status.count} - 2 + ${listStatus.count}] = new Client("${view.clientId}","${viewAccount}");
0
 

Author Comment

by:arkhls
ID: 22717678
Tnx for respond , I did  that already unfortunatelt it's not working
0
 
LVL 17

Accepted Solution

by:
sweetfa2 earned 250 total points
ID: 22717692
The problem is in the way you are calculating the index.  So why are you subtracting to from status.count.

Presumably as status.count is the outer loop the actual number of entries will be different for each iteration.  May I suggest a simple incrementing count value every time you add or maybe use clientObjectList.length as the index instead.

It may be clientObjectList.size() but I can't remember at the moment.

0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
servlet example 11 49
how to see all occupied ports on windows 10 laptop 15 76
servlet and mdb, jms error 1 32
java example issue 3 22
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question