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()?
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?
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.
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/
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?
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?
[ December 19, 2005: Message edited by: Andrew Mcmurray ] [ December 19, 2005: Message edited by: Andrew Mcmurray ]
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.
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?
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 ]
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.
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.
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?
posted 14 years ago
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
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.