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"?>
    <taglib
        version="2.0"
        xmlns="http://java.sun.com/xml/ns/j2ee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd">
        <tlib-version>1.0</tlib-version>
        <short-name>some-ui</short-name>
        <uri>http://some.com/tld/ui</uri>
        <tag>
            <name>ddm-template-selector</name>
            <tag-class>com.portal.taglib.DDMTemplateSelectorTag</tag-class>
            <body-content>JSP</body-content>
            <attribute>
                <name>classNameId</name>
                <required>true</required>
                <rtexprvalue>true</rtexprvalue>
            </attribute>
        </tag>
    </taglib> 
  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" "http://www.liferay.com/dtd/liferay-hook_6_2_0.dtd">
    <hook>
        <custom-jsp-dir>/custom_jsps</custom-jsp-dir>
    </hook>
    

That’s it.

Comments (0)

Tags: liferay


0 comments

Leave a Comment