• 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

Tag Handler - how to capture body of a tag?

 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I am a newbie to Tag Handling. I want to capture the body of a tag and then process it and then display it.

Suppose I have following in my code

<my:tahBody>
Display
</my:tahBody>

Now I want to get "Display ABC" and then process it to "yalpsiD" and then it shpuld be displayed...

I am really not sure how to do it.
somebody Please Help.

Thanks in advance,

Sushma
[ June 21, 2005: Message edited by: 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
Depends on the type of tag handler. And JSP 1.x or JSP 2.0?
 
Sushma Sharma
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to know both the way... with Simple tag, I don't know a single thing about body capturing.
I tried with the BodyTagSupport but even that is not working.

I overrided the doInitBody() method.
----------------------------------------------------------------
public void doInitBody() throws JspException{
try{
out.println("<BR>\t the bodyContent initially is " + bodyContent.getString());
StringBuffer bodyBuffer = new StringBuffer(bodyContent.getString());
bodyBuffer.reverse();
bodyContent.print(bodyBuffer.toString());
out.println("<BR>\t the bodyContent is now "+bodyContent.getString());
}catch(IOException ex){
throw new JspException("<BR> IOException = "+ex.toString());
}
}
-----------------------------------------------------

I returned EVAL_BODY_BUFFERED from toStartTag() method. Now I am calling it from my jsp.

<%@ taglib prefix="classic" uri="ClassicTags" %>
<html<body>
<Br> <Br> <Br>
<classic:classicBody>I have a Body</classic:classicBody>
<Br> <Br> <Br>
</body></html>

when I run this jsp, I get the following results :
--------------------------------------------------------
the bodyContent initially is
the bodyContent is now
------------------------------------------------------------
I don't know why it is not showing the body content in the result.
Please Help,

Thanks,

Sushma
 
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
SimpleTagSupport.getJspBody()
 
Sushma Sharma
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SimpleTagSupport.getJspBody() returns JspFragment.... which has only two methods getJspContext() and invoke(Writer) ... how would I get the body content from this?
Suppose I call my tag like this

<myTag:body>ABC</myTag:body>

I want to get ABC and do some processing with this content, like reversing or using it as key to a resourseBundle, how can I do that?

Regards,

Sushma
 
Sushma Sharma
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
getJspContext() returns the JspContext that is bound to this JspFragment.
we can get the writer from that, but first I need to read the content....
 
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
Here's the clue:

invoke(Writer)



read the javadoc for the above method.
 
Sushma Sharma
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did read the documentation for invoke(Write).
--------------------------------------------------------------
<code>
public abstract void invoke(Writer out)
throws JspException, IOException

Executes the fragment and directs all output to the given Writer, or the JspWriter returned by the getOut() method of the JspContext associated with the fragment if out is null.

Parameters:
out - The Writer to output the fragment to, or null if output should be sent to JspContext.getOut().
</code>
------------------------------------------------------------------

I have used getJspBody().invoke(null) before, but that is useful when I am not modifying the body.
I do understand that if I make changes to the body and write that to a writer and pass that write to invoke(), my problem would be solved. But I don't know the first step of the sequence....
Please help, I am desperate...
 
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

invoke(Writer out)



Did you try providing a StringWriter to this method so that you could capture the body?
[ June 22, 2005: Message edited by: Bear Bibeault ]
 
Sushma Sharma
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bear,
Thanks A lot for the solution... I was able to capture the body with StringWriter....
I am really very very thankful to you,

Regards,

Sushma
 
We should throw him a surprise party. It will cheer him up. We can use this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic