Blog Post


New taglib (tld) in Liferay Portlet

Sometimes in Liferay there is a necessity to implement your own jsp taglib (tld) for some portlets. At first sight this issue might seem trivial (just to follow a standard jsp tag library development process), in Liferay context it becomes not so easy. By default all Liferay jsp tags extend Liferay TagSupport class which keeps servletContext variable. The thing here is that this servletContext is a context of ROOT application. This means that all resources required for new taglib will be searched at ROOT application directory of your application container. Yes, we can store all needed resources at ROOT app, but this is a bad practice while Liferay core (ROOT app) should be always separate from customizations. In this article there is an approach and an example to overcome this issue.


  1. Let’s create a tld file with new tag description at this path %TOMCAT_DIR%/%PORTLET_DIR%/WEB-INF/tlds/new.tld:
    <?xml version="1.0"?>
  2. DDMTemplateSelectorTag at this path %TOMCAT_DIR%/%PORTLET_DIR%/WEB-INF/classes:
    public class DDMTemplateSelectorTag extends IncludeTag {
        private static final String _PAGE = "/html/taglib/ddm_template_selector/page.jsp";
        protected String getPage() { 
            return _PAGE;
  3. Many Liferay tlds have jsp represented views instead of standard java html generation. In the class below there is also a path provided that points to jsp view of current tag. But as mentioned in the summary: this page.jsp will be searched within %TOMCAT_DIR%/ROOT that is not so good. The easiest way to overcome this is to put page.jsp in liferay-hook directory (supposed that you have already a hook created via Eclipse). So let’s put page.jsp to the following path %TOMCAT_DIR%/%HOOK_DIR%/custom_jsps/html/taglib/ddm_template_selector/page.jsp.
  4. In order your hook to know the location of your custom jsps, you need to add to liferay-hook.xml:
    <?xml version="1.0"?>
    <!DOCTYPE hook PUBLIC "-//Liferay//DTD Hook 6.2.0//EN" "">

That’s it.

Comments (0)

Tags: liferay


Leave a Comment