Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Doubt in TagSupport (Classic Tag) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Doubt in TagSupport (Classic Tag)" Watch "Doubt in TagSupport (Classic Tag)" New topic
Author

Doubt in TagSupport (Classic Tag)

Bennet Xavier
Ranch Hand

Joined: Jun 19, 2008
Posts: 162
Hi All,

if i have a tag like..

<mine:iterator color="red">
${movie}
</mine:iterator>

i need to display the body of the tag should be displayed in RED color.

i don't know whether we can achieve this by Classic tag handler or not.

if there is any option please let me know.

Thanks In Advance,

Bennet Xavier.X


Thanks And Regards

Bennet Xavier. X
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

look at this

Hope this helps
raja ram
Ranch Hand

Joined: Mar 02, 2008
Posts: 169
Hi,

Basically we have to modify the body contents of the tag.

SimpleTagSupport Do not have any ways to gain access to tag's body contents
so we cant achive this through Simple Tags.

In case of Classic Tags Yes we can do this.by using Eval_BODY_BUFFERED return from doStartTag(). where the tag handler should extend BodyTagSupport class which will give you setBodyContent() and doInitBody().

Thanks
Bennet Xavier
Ranch Hand

Joined: Jun 19, 2008
Posts: 162
Thanks For Your reply...

Its working.
Somesh Rathi
Ranch Hand

Joined: Apr 27, 2006
Posts: 31
Hi,
For displaying the body content in RED ,
  • Define the attribute for the tag lets say color
  • Define the tag with Body & attribute in TLD file
  • Extend your tagHandler class from BodyTagSupport Class,

  • write the corresponding getter method for that attribute
  • Return the EVAL_BODY_BUFFERED constant from doStartTag()

  • Overrride the setBodyContent() & doInitBody() methods, that will be called after the doStartTag()
  • Give the implementation of doAfterBody() as follows

  • Get the JSPWriter object using bodyContent.getEnclosingWriter()
    Get the body string using bodyContetn.getString();
    Get the value of color attribute using the getter();
    Format the body using the color value
    Send the formatted content to the JSPWriter using the out.print()

    Hope this helps.. Please let me know if there is some diferent appraoch.

    REgards,
    Somesh
    Bennet Xavier
    Ranch Hand

    Joined: Jun 19, 2008
    Posts: 162
    Hi Somesh,

    thats correct, but i didnt extends BodyTagSupport.

    i just extend the TagSupport and defined the <font> tag in doStartTag() by using JspWriter.

    in doEndTag() using the JspWriter object i closed the <font> tag.

    thats it.

    Code Below...Please go through...let me know if there is any doubt.

    /************************************************************************/

    public int doStartTag() throws JspException{

    try {
    JspWriter out = pageContext.getOut();
    out.print("<font color=\""+color+"\">");
    }
    catch (IOException ex)
    {
    throw new JspTagException(ex.getMessage());
    }
    return EVAL_BODY_INCLUDE;
    }

    public int doAfterBody() throws JspException{

    our logic goes here....

    }
    public int doEndTag() throws JspException{

    try {
    JspWriter out = pageContext.getOut();
    out.print("</font>");
    } catch (IOException ex)
    {
    throw new JspTagException(ex.getMessage());
    }

    return EVAL_PAGE;
    }
    /*************************************************************************/
    Somesh Rathi
    Ranch Hand

    Joined: Apr 27, 2006
    Posts: 31
    Hi Bennet,
    Thats correct ! Your code seems to be much simpler. Thanks a lot for sharing !
    I just have one doubt . We are setting the font color in doStartTag() using the JSPWriter out object. As I understand we are using the same instance of JSPWriter object ( through which we have set the font ) in doAfterBody(). Please let me know whether my understanding is correct !

    Regards,
    Somesh
    Bennet Xavier
    Ranch Hand

    Joined: Jun 19, 2008
    Posts: 162
    Hi Somesh,

    i am not using the same object in the doAfterBody(), i am declaring different object in each method.(what i did is for demo, actually this is bad programming).

    but we need to declare a instance varible of type JspWriter and use that particular instance for your output.

    i think your doubt is "can we use different JspWriter Object for output or not".

    if this was your doubt, we can use any number of JspWriter object for output. but that again a bad programming.

    i think this is useful, if you are not comfortable with this please let me know.

    do reply...
    Somesh Rathi
    Ranch Hand

    Joined: Apr 27, 2006
    Posts: 31
    Hi Bennet,
    I got it. Thanks for clarification. PageContext.getOut() will return the current JspWriter stream being for response. In doStartTag(), we are sending some part of response & in doAfterBody() , we are sending the remaining response.
    Many thanks for your kind attention.
    Regards,
    Somesh
    Charles Lyons
    Author
    Ranch Hand

    Joined: Mar 27, 2003
    Posts: 836
    if i have a tag like.. i need to display the body of the tag should be displayed in RED color.... i don't know whether we can achieve this by Classic tag handler or not.
    As demonstrated, you certainly can do this using a tag handler... but why do you want to? I might be missing something, but if you want the entire contents of the tag in red, why not just do:or you might mean style="color:red" if it's foreground text colour you're changing. If using a background/foreground colour is a frequent requirement for this tag, and you still need the tag at all (i.e. it isn't just for inserting the colour!) then you can easily write the opening <div...> in doStartTag() and the closing </div> in doEndTag().

    Better yet use a CSS rule. For example, if your tag produces a bulleted list, this will highlight every option in the list with foreground red:So now the structural markup generated by your custom tag doesn't need to know anything about styling. This separation doesn't always work, but it's good if it can---that way if the styling needs changing by a Web designer not proficient with Java, they don't have to re-code and re-compile your custom tag! Try to avoid "oldie" HTML tags like <font> too as they are deprecated in favour of CSS styling like the above, and for good reason.

    It's a bit difficult to recommend anything more to do with styling without knowing what output you expect...


    Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
    Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / Amazon Amazon UK )
    Bennet Xavier
    Ranch Hand

    Joined: Jun 19, 2008
    Posts: 162
    Thats correct.

    Actually i want show the purpose of attribute in customtag, thats why i choose to show the content in the tag in colors, thats it. so that they can understand easily.

    Up to my knowledge, this type of UI part should not be done in tag handler class, is that right.

    Do reply...
    Charles Lyons
    Author
    Ranch Hand

    Joined: Mar 27, 2003
    Posts: 836
    You are right that separating the logic of your tags (XHTML or XML markup) from the presentation (with CSS) is ideal. It's not always possible to do this however, although in that case you'd be better marking with CSS classes or ids rather than deprecated HTML styling tags.

    There are plenty of examples of logical tags in the JSTL, so if you want to setup an example, just "borrow" the basic ideas from those. For example, in your case you want an iterator: the JSTL tag <c:forEach> does that. It has the "items" and "var" attributes, and also "begin" and "end" which can be used to control the number of iterations of the tag. Such examples also have the benefit of educating the reader as to how JSTL tags work, so they become familiar with those too. Just a thought
     
    GeeCON Prague 2014
     
    subject: Doubt in TagSupport (Classic Tag)