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().
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.
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 !
Joined: Jun 19, 2008
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.
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
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 / AmazonAmazon UK )
Joined: Jun 19, 2008
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.
Joined: Mar 27, 2003
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