JSTL vs. Struts taglib

The last couple weeks have been spent working on a couple different Struts applications, all of which will be deployed on Tomcat 5 with Hibernate. I’m finding that working with Struts is an enjoyable experience, the framework encourages true separation of HTML presentation and business logic and model.

As someone working on all aspects of the application, one of the things I butt my head against pretty freqently are the limitations of the struts HTML & Logic tags. For example, an application that shows a listing of widgets commonly shows each row representing a widget in alternating colors… grey, white, grey, white, etc. The Struts tags, specifically the logic:iterate tag provides no mechanism for determining what the current row number is. There are a number of solutions out there, almost all of them suggest using scriplets to create a simple counter in the loop:

String LIGHT_COLOR = "LightColor";
String currentColor = DARK_COLOR;
int i = 0;
<logic:iterate id="product" name="products">
if ( i % 2 == 0) {
currentColor = "BLACK";
} else {
currentColor = "WHITE";
<tr ALIGN="center" class="<%=currentColor %>">
  <bean:write name="product" property="label" />

Of course, the entire reason tags are used is so that you don’t have to use scriptlets, so it seems like a pretty ugly hack to me. Fortunately I’m using Tomcat 5, so I stumbled upon the JSTL forEach tag, which provides effectively the same functionality as the Struts iterate tag, with the benefit of an index and row status so that the above code becomes:

<c:forEach var="product" items="${products}" varStatus="status">
   <c:when test="${status.index % 2 == 0}">
   <tr ALIGN="center" class="dark">
   <c:when test="${status.index % 2 != 0}">
   <tr ALIGN="center" class="light">

which is a bit more simple, with the main benefit that you don’t have use any Java code.

Further, it seems that there is alot of overlap between JSTL and the Struts tag library, which isn’t a bad thing. I’m guessing that not everyone has the opportunity to use JSTL since it requires a web container that supports JSP 2.0, but for those who are deploying to JSP 2.0 web containers, how much do you use JSTL versus the Struts tags? Are you phasing out Struts tags for JSTL? Keeping with Struts tags to maintain a sort of purity to your application?

20 thoughts on “JSTL vs. Struts taglib”

  1. > JSTL since it requires a web container that supports JSP 2.0

    Not true, JSTL 1.0.x will run on any JSP 1.2 container. It is true that JSTL 1.1.x requires a JSP 2.0 container.

    I’ve been using JSTL ever since it was released. I try to use it whenever I can to replace Struts tags. My rule is – whichever tag requires less typing, that’s the one I use. 😉

  2. I don’t like the design of struts tags, and I use JSTL since the 1.0 version (with Tomcat 4).
    The problem is that JSTL cannot replace the struts html tag library; what is missing at the moment is a good tab library of UI widgets.

    JSF aim is to be that type of library, but I personally dislike very much the complexity of JSF.

    My choice is to use JSTL for logic tags and to build a tag library of UI widgets bi myself.

  3. > the logic:iterate tag provides no mechanism for determining what the current row number is.

    that is false, read the doc you linked to again. http://jakarta.apache.org/struts/userGuide/struts-logic.html#iterate :
    “indexId: The name of a page scope JSP bean that will contain the current index of the collection on each iteration.”

    That said, don’t use the Struts logic:equals and similar, they have horrible performance problems and we, the university of florida, got a 2.5 performance boost simply by replacing logic: tags with the equivalent JSTL tag.

  4. If you are going to iterate over some collection to put it in a table, you should definitely check out the displaytag at sourceforge. I’m a bit amazed that Matt didnt point you to that 🙂 (allthough it does mean adding another library)


  5. I agree to your approach – personally I prefer the following (maybe a bit more elegant) coding:

  6. The JSTL taglib is WAY more useable in my opinion – The struts logic taglib simply does NOT do what I need as a “view layer” developer… sheesh! – you cant even test multiple values in logic tags

  7. this is one of the useful method which jakarta struts has introduced . I found very easy to work with it.

    If u have any other information then please send to me.I will be very thankful to u .


  8. See also this bugzilla page (link below). Although a little old, there is an example of how to achieve this using the struts taglib with the indexID attribute. (You still need to use a scriptlet for the modulus bit though).


    It seems McClanahan had considered the issue at one stage, however the “official” answer at the bottom is to use the jstl.


  9. You don’t actually need to know what row you’re on if you use the old “swap” trick.

    Define bean tags for your alternate background colors. define two other beans for activeColor and nextColor.

    At the end of the iterate tag
    define a new bean called swap defined as activeColor, redefine nextColor as activeColor and redefine nextColor as swap.

    I’d show this as sample but your page doesn’t allow it.

  10. Hey, John, the memory leak is due to tag pooling in Jasper… And as far as I know, tag pooling can be disabled, so JSTL remains a good choice vs Struts taglib, doesn’t it…?

  11. When it comes to picking a taglib (struts/jstl/velocity) irrespective of the framework used for a webapp (struts/spring/…)
    what do you think is a good taglibrary to use? Does anyone have a say on this?

  12. I have a object with its properties.

    and I use JTSL to output

    and out is: myObject.properties

    but when I use struts and JSTL,


    ‘ />

    then output is: instand of myObject.properties and i can’t edit a product that is got by Id

    help me!!!

    thanks very much.

  13. @Aaron Johnson in your bit example,you told JSTL is good as compare to struts tags.But in your bit,the code which is given to struts tags is very complex.In this case you can use a tag instead of a

    like as JSTL tags.Struts has all JSTL tag function and also additional function too.

  14. what is the Spring equivalent of in jsp?

    is in struts
    How to convert this in spring so as I should use this in JSP?

Leave a Reply

Your email address will not be published. Required fields are marked *