This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Custom if then else tags?  RSS feed

 
Andrew Mcmurray
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all ,

I am writting some custom if then else tags and I am wondering if there is a way to check for a child tag like there is a way to check for a parent. For example I want to be able to check from my if tag, if a then tag exists. I don't won't to allow <pre:myIf test="true">
some content
</pre:myIf test="true">

I want to make sure there is a myThen tag nested. For example

<pre:myIf test="true">
<pre:myThen>
some content
</pre:myThen>
</pre:myIf>

I saw there is a getParent(), but I did not see a getChild()?

Any thoughts?

Thanks,

AMD
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66188
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved to the JSP forum.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66188
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why re-invent the wheel rather than use the JSTL conditional tags?
 
Andrew Mcmurray
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would, but this is an exercise for a class. I have most of it written I just can't figure out how to handle the case of the then and else tag not being nested. Do you think using getBodyContent() and converting it to a string and then searching for the tag your expecting to be nested would work?

Thanks,

AMD
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66188
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The best way to handle this would be for the parent tag to have a method that the child tag can call (as the child can easily find the parent). The parent tag could then detect whether the child registered itself in this way either in the end tag handler for a classic tag, or after processing the body in a simple tag.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the first edition of "Core Servlets and Java Server Pages" there is a chapter on building custom tags. In it the book walks you through building a set of if-then-else tags.
http://pdf.coreservlets.com/
 
Andrew Mcmurray
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies, but how can you tell if the child tag does not exists. Like this case <pre:myIf condition="true">
some content
</pre:myIf>

I don't want the content to be displayed b\c it is not in the then tag?

Thanks,

AMD
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66188
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but how can you tell if the child tag does not exists.


As I said, if the child does not "register" itself with the parent, you'll know it's not there.
 
Andrew Mcmurray
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry Bear. I am not sure what you mean when you say register yourself with the parent. Can you explain? Btw I am using the JSP 2 SimpleTag.

Thanks,

AMD
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66188
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please re-read my 2nd reply to this topic.
 
Andrew Mcmurray
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I might be getting it. Do you mean that the child calls a method of the parent that sets a flag or something in the parent letting the parent know if the child exists? Is that what you mean by registering?

Thanks again

AMD
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66188
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly. This technique also works well when the parent needs data available to the child tags.
 
Andrew Mcmurray
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bear I just gave it a try and I think I am close, but missing something small. Here is the code for the if and then tags. I set the hasChildRegistered property of the if tag in the then tag. And initalize the property to false in the if tag. This prevents cases like this
<pre:if condition="true">
some content
<pre:if>

But it also seems to keep the good cases from working also?

Any thougts?

Thanks

AMD

If tag


Then Tag


[ December 19, 2005: Message edited by: Andrew Mcmurray ]
[ December 19, 2005: Message edited by: Andrew Mcmurray ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrew,
If you're going to post more than a line or two of you code, please use the UBB Code tags. They will help to preserve your indenting which makes the code much easier to read. There is a button on the edit page to help with this.

They can be added after the fact by clicking on the edit icon.
 
Andrew Mcmurray
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oops sorry!
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66188
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Think about this fragment.
 
Andrew Mcmurray
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it does not even evaluate the body of the tag b\c hasChildRegistered is initialized to false, right? I set it to true in the then tag, but I guess that tag is not even being called? This prevents the bad case, but the good case does not work. If I initialize hasChildRegistered to true then the good case will work, but the bad case won't?

Good case



Bad Case

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66188
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


You are checking the flag before evaluating the body of the tag (where the child tags are). How do you expect the flag to be set before the child is even evaluated?
[ December 19, 2005: Message edited by: Bear Bibeault ]
 
Andrew Mcmurray
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats what I thought the problem was, but I didn't want the body of the tag to be evaluated unless the body was the else tag. So should it be something like this?



Thanks,

Andrew
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66188
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you can't check for the existance of the child prior to evaluating the body using this technique.

If you want to check the structure of the tags prior to evaluation, you'll need to implement a TagLibraryValidator. But that's a big can of worms that I'm not sure you want to get into at this point.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66188
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you really want to make sure that the body is not sent to the page in the failure case (not sure why that's a big issue since you are going to throw an exception in that case), another technique would be to invoke the body on a StringWriter which you then discard. That way, the child will be invoked (so you can check the flag) without emitting output to the page's output stream.

Kind of messy -- but then so is what you are trying to do. I undestand it's an academic exercise, but it is rather pointles except as a means to become familiar with tag mechanics.
 
Andrew Mcmurray
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bear,

Why would an exception be thrown is this case, since the body would be evaluated?



Thanks,

AMD
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66188
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That depends how you code the tag. If you evaluate the body (discard the results) and the child has not been invoked to set the flag, you could throw the exception. I guess at this point I'm a bit confused about what you are still confused about.
 
Andrew Mcmurray
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bear,

Sorry to be confussing . I'll try to give some examples.

This example should work ok



This example should not work it should skip the body b\c no then tag

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66188
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, yes. That's what you would like. There's enough info in this thread to easily make that happen. Give it a try and show us the code.
 
Andrew Mcmurray
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok when I get home I will post the code. I am still confused about how the bad case will not display the body, giving that you have to look at the body to have the child set the flag on the parent?

Thanks,

AMD
 
Andrew Mcmurray
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok Bear I think I figured it out, but it seems a little clunky. I call the getJspBody.invoke(buffer) the first time to set the child if it is there. Then if it is there I call getJspBodyInvoke() a second time to output. Seems cluncky to have to call it twice. Here is the code of the tages, let me know what you think? Thanks AMD

If tag


Then Tag

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66188
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, if you read back you will see that I said that this would be on the messy side. But then your requirements call for it. Again, I understand that this is an academic exercise, but I would never define a tagset that works in this clunky fashion.
 
Andrew Mcmurray
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great! Thanks for your help Bear

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