• 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

doStartTag()

 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This example is from the HFSJ book, chapter:10,page:530. Here I am not getting, why we need to add two bold lines(which I have showed) in doStartTag()? Variable movieCounter is increment by 1 after doStartTag so when doAfterBody() calls it prints "Saved" & "Amelie". It never prints "Spiderman".
I don't undersatnd. Or I am missiing something. Please anybody can clear my doubt?


 
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's what it prints or that's what you ASSUME it prints?

By the way, I don't think the attribute value is supposed to be in quotes, otherwise your page will simply print that text in quotes each time.

This example is doing something nearly identical to the c:forEach tag. I recently discussed how the c:forEach tag works in another thread.

The tag is merely saving the value as a page attribute (aka scoped variable) so that it may be referenced from the JSP. The increment doesn't happen until after the attribute is set, so each of the three movies will get a shot at being evaluated in the JSP.

I always encourage folks to try out code. I understand that it may seem intimidating and it might take a couple hours -perhaps even frustrating hours- but once you get over that hump you can test and play around with all kinds of code and you will save countless hours of reading and rereading because you'll be able to understand concepts much more quickly by seeing them in action.

Best of luck!
 
Marc Peabody
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, your question was specifically why those bold lines are needed.

Well, technically you are correct that omitting those two lines should still print all three movies. But if you omit those two lines the body will get evaluated once first without the scoped variable set. If the body holds nothing but a ${movie} then you would not notice the difference but if the body holds more than just that you would get a bunch of extra garbage from everything else in the body.

I don't have HFSJ in front of me, but there should also be a check in doAfterBody() to see if the list has any more values and, if not, return SKIP_BODY so the tag will stop evaluating the body.
[ December 04, 2007: Message edited by: Marc Peabody ]
 
dolly shah
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not getting you Marc. My question is in doStartTag() variable is increment by "1". So in doAfterBody() it prints "Saved" instead of "Spiderman". Why the output is ["Spiderman", "Saved", "Amelie"]?



 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Body is ${movie}

(Step1)doStartTag() excecutes in the TagHandler class
(Step2)${movie} body is evaluated which prints Spiderman
(Step3)doAfterBody() is excecutes and returns EVAL_BODY_AGAIN
(Step4)${movie} body is evaluated which prints Saved
(Step5)doAfterBody() is excecutes and returns EVAL_BODY_AGAIN
(Step6)${movie} body is evaluated which prints Amelie



Remember Body is evaluated Once before doAfterBody
 
Marc Peabody
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by dolly shah:
I am not getting you Marc. My question is in doStartTag() variable is increment by "1". So in doAfterBody() it prints "Saved" instead of "Spiderman". Why the output is ["Spiderman", "Saved", "Amelie"]? [/CODE]


James Mark's step by step should help.

You're saying that the doAfterBody is printing things. It doesn't. There is no printing happening in that doAfterBody. What gets printed is the evaluated body of the tag - as in the <someTag>BODY GOES HERE ${movie}</someTag> part of the JSP.

Also, I think you're thinking the increment is more responsible than it really is. Consider the code below:
[code] String[] names = {"Anna", "Bob", "Charlie"};
int index = 0;
String name = names[index];
index++;
index++;
System.out.println(name);
[/code]
What gets printed? It doesn't matter that I incremented index twice. My name variable was set to "Anna" and so that's what prints when I reference the name variable.

This is exactly what's happening in the tag class. I believe that you're thinking the increment will change the scoped value, as if you would have expected my code here to print "Charlie". Incrementing the number value doesn't change the scoped variable. The number is incremented so that the next time the scoped variable is set (which will happen in doAfterBody), it's got the number already lined up and ready to go.
 
dolly shah
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to both of you. I got it.
reply
    Bookmark Topic Watch Topic
  • New Topic