my dog learned polymorphism*
The moose likes JSP and the fly likes Why is c:forEach working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Why is c:forEach working" Watch "Why is c:forEach working" New topic
Author

Why is c:forEach working

Priety Sharma
Ranch Hand

Joined: Jun 10, 2008
Posts: 156

I used the following tags to loop in my jsp:


I am using Tomcat 5.0.28.
I dont have jstl libraries in any of my tomcat folders.
Has c:forEach nothing to do with JSTL.
I am accessing a bean using the above tags.
A little confused as to why this is working.


Priety.
Priety Sharma
Ranch Hand

Joined: Jun 10, 2008
Posts: 156

Sorry post needs to be corrected.
I think its currently working just because of EL and nothing to do with JSTL.
Am I right?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61448
    
  67

Of course <c:forEach> is a JSTL tag.

What do you mean by "working"?

Is it actually iterating over the collection? Or is it just ignoring the tags?

What's in the HTML that's being sent to the browser?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Priety Sharma
Ranch Hand

Joined: Jun 10, 2008
Posts: 156

Hi Bear,

I am trying to do away with using scriplets. Thanks to you suggestion on my earlier post.
So I want to use JSTL for looping and also EL for that purpose.

Here is what I am doing:
My servlet code:
I am creating a few beans here and naming them as "bkinfobean" and appending an integer value to the end of the name of the bean.


In my jsp I want to iterate over the beans and get the data which is bookname, author and number of copies.

This is what I have currently have in my jsp:


Now I want to replace items="${bkinfobean0.bkinfo}" with syntax which gets the right name instead of that hardcoded bkinfobean0.
I mean the interger at the end of the bean name should get replaced by 0 and 1 so on depending upon the number of beans added through the servlet.
This is where I dont now where to go?
Or should I put the beans in an arraylist in the servlet itself and add the arraylist to the servlet and then iterate over the arraylist using JSTL to access the bean properties?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61448
    
  67

Priety Sharma wrote:and appending an integer value to the end of the name of the bean.


Why?

In my jsp I want to iterate over the beans and get the data which is bookname, author and number of copies.

Then they'll need to be in a collection such as a List or array -- just like any other Java code.

Or should I put the beans in an arraylist in the servlet itself

Of course. Why would you think to do anything else?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61448
    
  67

Oh, and a suggestion: vowels don't cost extra. call it bookInfo. There is no need for useless abbreviations.
Priety Sharma
Ranch Hand

Joined: Jun 10, 2008
Posts: 156

Hi Bear,

I am appending the integer value to the end of the bean names in the servlet code to give distinct names to the beans inside the loop.
Otherwise when I put the bean in the request scope inside the loop only one will be added, the last one.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61448
    
  67

You don't create a scoped variable for the individual beans. You create it for the collection of beans.

If you were going to pass a collection to a Java class would you append numbers to variable names? Or would you use an array or List? No difference here.
Priety Sharma
Ranch Hand

Joined: Jun 10, 2008
Posts: 156

Thanks Bear,

I will change my code to add the beans in an arraylist and put the arraylist in the request scope.
But will there be no issue of the id of the bean then?
Anyway let me try and I will post the new code again.

Thanks a lot. Getting to learn a lot here.
chima alaebo
Greenhorn

Joined: Sep 16, 2010
Posts: 3
sharma you need to put each bean in an arraylist,store the arraylist in in the request scope,then from the jsp you use something like
for each element in the arraylist you then access which property you want,u acess the elements with ${arraylist[element]}
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61448
    
  67

chima alaebo wrote:,u acess the elements with ${arraylist[element]}

Firstly, please be sure to use real words when posting to the forums. The word "you" is spelled y-o-u. Thanks.

Secondly, nowhere was a double-dimensioned list mentioned, so your example is likely more confusing than helpful.
Priety Sharma
Ranch Hand

Joined: Jun 10, 2008
Posts: 156

Hi Bear,

Its working now. Though I have a doubt about how EL works here.

Here is what I have now in my servlet:


Here is what I have in the jsp:



This works perfectly and gives the desired output.

Thanks for all the inputs.

Now my question is:
How does EL work here and manage to access the javabean in the arraylist?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

What do you mean, how does it work? That's what EL *does*.

Are you asking how it's implemented?
Priety Sharma
Ranch Hand

Joined: Jun 10, 2008
Posts: 156

Ok let me try to elaborate.

c:forEach works because of JSTL.

Now

refers to the arraylist put in the request scope by the servlet.
The objects in the arraylist are java bean objects.

So when one declares :


The var will refer to the members of the arraylist. I hope I am right.
I am trying to understand how c:forEach and EL work.

and after that:


This is how the bean properties are getting accessed.

So is EL doing the casting from object to the java bean class(BookSelectedforDel) automatically?

I am asking this because whatever I have written and working is almost like trial and error.
Its mostly mechanical.
I dont have a proper understanding of both the c:forEach loop and EL.

Though I now have some hang of c:forEach syntax and usage.

Where can I read about both c:forEach and EL to be clear about them?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

http://download.oracle.com/javaee/1.4/tutorial/doc/JSTL3.html

http://faq.javaranch.com/java/SpecificationUrls
http://www.ibm.com/developerworks/java/library/j-jstl0211.html

It's critically important that you, as a developer, be able to find this kind of information on your own, however.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61448
    
  67

Priety Sharma wrote:So when one declares :


The var will refer to the members of the arraylist. I hope I am right.

Mostly. In this case bkinfo (why not bookInfo? I mean really!) is a page-scoped variable set to the current iteration item during each pass of the iteration.

and after that:


This is how the bean properties are getting accessed.

So is EL doing the casting from object to the java bean class(BookSelectedforDel) automatically?

If bkInfo refers to a bean, bkinfo.bookname refers to the bookname property of the bean.

I am asking this because whatever I have written and working is almost like trial and error.

Where can I read about both c:forEach and EL to be clear about them?

Get a copy of the JSTL specification and the EL specification and stop guessing.
Priety Sharma
Ranch Hand

Joined: Jun 10, 2008
Posts: 156

Hi David and Bear,

Thanks for the information.

As you said David that I should be able to find information on my own.
I do always try that and will take better efforts for the same.
Just before you replied.
I was reading this link:
http://download.oracle.com/docs/cd/E17802_01/j2ee/j2ee/1.4/docs/tutorial-update2/doc/JSPIntro7.html


I will do the reading and also try to get copies of JSTL and EL specifications.

Thanks again.
dileep keely
Ranch Hand

Joined: Jun 28, 2010
Posts: 91
Thought this may help you
EL:Elimination to java syntaxs in JSP

It checks the syntax vaildity not the existence of variable.

-->Translation (which involves compilation):
The abstract class javax.servlet.jsp.el.ExpressionEvaluator has a method evaluate(java.lang.String, java.lang.Class,javax.servlet.jsp.el.VariableResolver, javax.servlet.jsp.el.FunctionMapper)

checks for syntax error.

--->RunTime:
At runtime the String representing the expression(in your case:bkinfobean) is sent to a method called resolveVariable()
which uses
javax.servlet.jsp.JspContext 's public abstract java.lang.Object findAttribute(java.lang.String);
Adding on this finAttribute checks through the scopes in the following order.
page,request,session,application.(In your case request)

the returned object is sent to the outputstream via an out.print();
Note:Even if the variable doesn't exists sensible defaults are provided.
Priety Sharma
Ranch Hand

Joined: Jun 10, 2008
Posts: 156

Hi Dileep,

Thanks for the info.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why is c:forEach working