• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can you use c:forEach loop to display output of multiple arrays or arraylists?

 
S Shiva
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
This is my first post....
I have been learning JSP and Servlets when I came upon JSTL and EL concepts.. I am new to JSP...
From what I understand JSTL and EL was introduced in JSP so as to avoid using scriplets in JSP because it clutters everything(business logic as well as presentation logic) in a JSP,which was originally designed for presentation purposes...

But the prefix:forEach loop has me confused...I have tried using it to display arrays,lists,string,arraylists,vectors,etc... for practice and noticed that I have to use for every new array,string...
So if I have 10 different arraylists..I will have to use 10 forEach loops...It kind of makes the page too long and lot of efforts...If I use scripletscan use a normal for loop and use the and get data of all 10 arraylists...

When I needed to get 100's of data to get from 14 arraylists from databases, I simply used


The same if I used c:forEach is like this...



See I have used 2 forEach loops for 2 different arraylists... arraylist names are same as in first example..I stored them in request object.

I want to folow java programming conventions which states to use minimum possible or no scriplets in jsp pages...
Or is there a way to make prefix:forEach to behave like a normal for loop and get all 10 arraylists's data using single forEach??Am I using the loops wrong...If wrong how can I use a single forEach to display elemnts in both arraylists as that of simple for loop as given above..Is it possible??
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Firstly, if you are using the c-rt library, you're definitely using the wrong library. That's a very very old version of the JSTL 1.0 not compatible with modern servers. Be sure that you are using at least JSTL 1.2.

Secondly, if you are looping through multiple parallel arrays, I'd say that your model is whacked and needs refactoring. Why the parallel arrays? That's a red flag for a poor model design.

But yes, when it comes down to brass tacks, you can use the JSTL to iterate through multiple arrays in one loop as you can get the array index for the iteration loop on each pass.
'
But before resorting to that, I'd carefully rethink the design of the data.
 
S Shiva
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Firstly, if you are using the c-rt library, you're definitely using the wrong library. That's a very very old version of the JSTL 1.0 not compatible with modern servers. Be sure that you are using at least JSTL 1.2.

Secondly, if you are looping through multiple parallel arrays, I'd say that your model is whacked and needs refactoring. Why the parallel arrays? That's a red flag for a poor model design.

But yes, when it comes down to brass tacks, you can use the JSTL to iterate through multiple arrays in one loop as you can get the array index for the iteration loop on each pass.
'
But before resorting to that, I'd carefully rethink the design of the data.



Ok..So I am now using jstl 1.2...

You are saying that my method of parallel arrays needs refactoring then how do i get data from database..any help would be appreciated...Thanks..
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Without knowing the nature of your data, it's impossible to say what a good model may be. But Java is an OO language -- parallel lists or arrays to hold related data is more suited to FORTRAN than to Java.

And one of the very (very) important keys to keeping JSPs from becoming hioly nightmares, is to have proper modeling of the data.

 
S Shiva
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Bear

My data comes from informix database systems... The data is of the sort of broadband customers's details...
There will be consumer no.,Name,Bill No., bill Dated,Date Paid, Usage,etc... I stored all these info in an arraylist(one for each...)
I am not to use any frameworks as it is a very small project...
Actually I have finished it using scriplets in jsp... But now I want to use java programming conventions..
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
S Shiva wrote:
There will be consumer no.,Name,Bill No., bill Dated,Date Paid, Usage,etc... I stored all these info in an arraylist(one for each...)

That's not a great way to store the data. A more OO approach better suited to Java and to JSP would be to have a bean that had properties for all the values assocviated with one consumer. Then you would have one list of those beans you could iterate over.

 
S Shiva
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:A more OO approach better suited to Java and to JSP would be to have a bean that had properties for all the values assocviated with one consumer. Then you would have one list of those beans you could iterate over.





So the bean you are talking about, is it EJB?? If yes I tried to learn it but seemed a lot complicated... One of my trainers suggested me to use Hibenate instead of EJB(because he told me Hibenate is much simpler than EJB)...
When I was learning Java, I kinda planned to learn jsp & servlets and hibernate/spring/ejb... Now that I have researched quite a bit on them, I can say that hibernate simplifies a lot of database access work. Then I would learn EJB and spring... But for current project I cannot use frameworks as it will make the project/application heavy(according to client and my team leader)...
So Which one wil be better to learn first?
Any good examples availabe for it.. right now I am learning through java2s.com

Thanks Mr.Bear Bibeault
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
S Shiva wrote:So the bean you are talking about, is it EJB?

No. Not at all. EJB is a whole different concept.

If you are going to be writing Java, and especially for the web, you need to know what a plain old JavaBean is.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!