This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer Study Guide: Exam 1Z0-830 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
  • 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Doubt in TagSupport (Classic Tag)

 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
look at this

Hope this helps
 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks For Your reply...

Its working.
 
Ranch Hand
Posts: 31
Hibernate Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
    Posts: 162
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 31
    Hibernate Eclipse IDE Oracle
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 162
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 31
    Hibernate Eclipse IDE Oracle
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
     
    Author
    Posts: 836
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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...
     
    Bennet Xavier
    Ranch Hand
    Posts: 162
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 836
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
     
    Squanch that. And squanch this tiny ad:
    Gift giving made easy with the permaculture playing cards
    https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
    reply
      Bookmark Topic Watch Topic
    • New Topic