• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why is c:forEach working

 
Ranch Hand
Posts: 156
Android Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 Sharma
Ranch Hand
Posts: 156
Android Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry post needs to be corrected.
I think its currently working just because of EL and nothing to do with JSTL.
Am I right?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Priety Sharma
Ranch Hand
Posts: 156
Android Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, and a suggestion: vowels don't cost extra. call it bookInfo. There is no need for useless abbreviations.
 
Priety Sharma
Ranch Hand
Posts: 156
Android Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 156
Android Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 156
Android Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you mean, how does it work? That's what EL *does*.

Are you asking how it's implemented?
 
Priety Sharma
Ranch Hand
Posts: 156
Android Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 156
Android Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 156
Android Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dileep,

Thanks for the info.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic