Tag Libraries and XML, what's the point?

Discussions

General J2EE: Tag Libraries and XML, what's the point?

  1. Tag Libraries and XML, what's the point? (2 messages)

    I use custom html tags whenever possible. A rule of thumb that I work with is "no looping in the JSP". (I'd rather use presentation servlets but management won't let me) Anyway, when I use a custom tag I simply import it at the top of the JSP...
    <% import com.CustomTag %>

    And then use its static methods. All of te custom tag methods take an HttpServletRequest as the parameter and return a String. So I can insert custom data into a JSP like so...

    <%=CustomTag.getUserName(request)%>

    It's very simple and I have no reason to extend TagSupport. Which leads to my question, what advantage is there to extending TagSupport? Why should I bother with defining a ".tld" xml doc for my custom tags? It seems to me that this is just another excuse to use XML (the Holy Grail of modern computing)

    Am I missing something here?
  2. CustomTag is a custom tag?????? Appear to be a plain class Java. I would not use custom tag to show to an attribute or request parameter.

  3. Well, if all you want to do is just get attributes, than custom tags don't buy you much. They are mostly useful for rudimentary looping/conditionals, although some use them to create custom-tag versions of HTML form fields, e.g.

    <mytags:textfield property="foobar"/>

    This can be much more compact than:

    <input type="text" name="foobar" value="<% request.getAttribute("foobar");%>">

    and give you more flexibility, etc.

    But, you are right in that just because it's XML and OO it can't be abused. It can be severely abused and can lead to needlessly complicated ways of doing simple things.

    However, if you are building a large or complex website (where complex means, the UI is very complex, with lots of "components" appearing in multiple contexts), taglibs is a good way to create re-usable components without having to be modifying servlets and redeploying them and all that. Most complicated sites involve the HTML programmer's dreaded "change a character, reload, repeat" and putting as much presentation code in JSPs helps that. AND, if you put your presentation code in JSP, it can be very handy to put some presentation logic in JSP in the form of custom tags. For example, you typically have the situation where a user enters bad data on a form and they need to be told about it. Your form could be:

    <mytags:ifError>
    <font color=red><mytags:getErrorMessage/></font>
    </mytags:ifError>
    <br>
    <form>
    .....
    </form>

    as opposed to:

    <%
        if ( (request.getAttribute("formError") != null)
           && (request.getAttribute("formError").equalsIgnoreCase("true")))
        {
            %>
            <font color=red>
            <%= request.getAttribute("formErrorMessage"); %>
            </font>
            <%
        }
    %>