2

hi all i just want to create a simple jsp tag

such as :

abc.tag

<div>
    <table>
        ....
    </table>

    body content goes here

</div>


so when in the other pages i can use my tag like this
<tag:abc>
     acutally body content
</tag:abc>

how can i do it without java code??? coz the functionality for my tag is siimple, i don`t want to write a bunch of stuff for it

1

2 Answers 2

9

sigh

This is why JSP gets a bad wrap.

JSP 2.0 has a feature called "Tag Files". They let you create JSP tags using JSP.

What you want to do is trivial.

You will need to create a file named "abc.tag", and put it in, for example, WEB-INF/tags within your WAR.

The contents are simple for this case.

<div>
    <table>
        <jsp:doBody/>
    </table>
</div>

To use the file in your JSP:

sample.jsp:

<%@ taglib tagdir="/WEB-INF/tags" prefix="tag" %>
<tag:abc>
    actual body content
</tag:abc>

That's it! Shazam. The only caveat about tag files is that you can not use JSP scriptlets within you tag file tags. But, then, you shouldn't be do that anyway -- it's not a big deal.

Look up tag files for more details (like passing parameters and such).

Tag files, JSTL and EL make JSP 2.0 one of the best markup languages around.

Sign up to request clarification or add additional context in comments.

1 Comment

@Will Hartung there is any place can look for difference between jsp 1.0 and 2.0
0

i have no idea what you're trying to do... but from what it looks like, i think you're trying to edit the font color of specified words? if so then you'd best be using CSS and perhaps JQuery. JQuery could be overkill though, so maybe just CSS

2 Comments

jsp 2.0 tags can allow people create a tag without writing any java code.. for my case, i want to write a re-useable tag.. imaging you always want to put some thing on top of your table. for createing a tag to do this,t u don`t need to always create that stuff for all your table, you just rewrite the table tag.
I kinda get you now, but i still think it would be more convenient to just use CSS. If i wanted to do something more complicated, i might consider this, however, for something as simple as colouring text, i'd just go with CSS as it's simpler. this is just my opinion but feel free to disregard it cause i'm not that experienced anyway haha

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.