This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes JSP and the fly likes Tag Handler - how to capture body of a tag? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Tag Handler - how to capture body of a tag?" Watch "Tag Handler - how to capture body of a tag?" New topic
Author

Tag Handler - how to capture body of a tag?

Sushma Sharma
Ranch Hand

Joined: Jun 02, 2005
Posts: 139
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 ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61106
    
  66

Depends on the type of tag handler. And JSP 1.x or JSP 2.0?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Sushma Sharma
Ranch Hand

Joined: Jun 02, 2005
Posts: 139
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61106
    
  66

SimpleTagSupport.getJspBody()
Sushma Sharma
Ranch Hand

Joined: Jun 02, 2005
Posts: 139
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

Joined: Jun 02, 2005
Posts: 139
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61106
    
  66

Here's the clue:

invoke(Writer)


read the javadoc for the above method.
Sushma Sharma
Ranch Hand

Joined: Jun 02, 2005
Posts: 139
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61106
    
  66

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

Joined: Jun 02, 2005
Posts: 139
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
 
Don't get me started about those stupid light bulbs.
 
subject: Tag Handler - how to capture body of a tag?