Search code examples
jspjsp-tags

How to let multi jsp tag libs use the same prefix?


In my jsp, I have used 4 tag libs. The declaration is:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="cms" uri="/WEB-INF/tags/cms/tag.tld"%>

Each of them has a unique prefix.

But I want to use a single prefix for all of them, so I change them to:

<%@ taglib prefix="cms" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="cms" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="cms" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="cms" uri="/WEB-INF/tags/cms/tag.tld"%>

It reports errors.

Is there any way to do this?


Solution

  • Each taglib directive imports tags under the tag library descriptor called as TLD. A TLD will have set of tags identified by tag name. The tag name here is not universally unique. Two TLD's can have the tags with same name. That is why TLD's have namespace which is nothing but TLD URI (http://java.sun.com/jsp/jstl/core,http://java.sun.com/jsp/jstl/functions, ...).

    So, while using any tag one should clearly tell from which TLD the tag has to be loaded. And, because it is difficult and not readble to give the URL before every tag, we give an alias to the URI with the help of prefix in tag lib directive.

    So thats the reason you cannot have same prefix for multiple TLD's.