Search code examples
javajspjsp-tags

How to include only once tag on the page


I have implemented some component. I used jsp custom tags. But I've implemented 2 tags which make hard to use this in the future by others programmers. How can I improve my code to use only one tag. My tags are .tag files.

Below is what I need:

<c:foreach...
   <u:myTag1/>

<u:myTag2/>

I want to display repeatable myTag1 and only once myTag2

It would be nice to have such things (without myTag2):

<c:foreach....
   <u:myTag1>

Solution

  • If you want to use only one tag then you're in the realms of creating a new tag in the TLD and implementing another Tag. Hopefully you should be able to refactor Tag1 and Tag2 to put common functionality somewhere e.g. Helper class or abstract class, so the duplication between Tag1, Tag2 and Tag1AndTag2 is minimal.

    Alternatively you may want to look at JSP fragments or some other templating system, so pages can include the common JSP in. However, the tag solution sounds better - if you're sure that offering a third flavour of tag (combing two of them) is the way to go. You may make the decision to stay with just Tag1 and Tag2.

    I'm assuming you're using just standard JSP tags here. If you were using JSF 2 you could look at composite components or something, which may save some pain.