Liferay JSP Tag Libraries Introduction

Liferay have given many tag libraries to make development easy and rapid. Liferay have developed their own tag libraries which we can use extensively in liferay development.

What is Tag Library?

Tag library is set of JSP tags and each tag have its own syntax and required attributes.

What is JSP Tag?

JSP tag is way to avoid the writing repeated code snippets in the JSP pages. Each tag have its own piece of presentation markup(html/java script/css/java code) code which can be encapsulatesas a separate part and it will be identify by the specific notation called Tag.

When we use the tag in the JSP page it will be replaced by respective code snippet/presentation markup at the time of page rendering.

When we work with JSP pages we aware of JavaServer Pages Standard Tag Library (JSTL)

Whats is JSTL?

TheJava Server Pages Standard Tag Library (JSTL) is a collection of useful JSP tags which encapsulates core functionality common to many JSP applications.

We have many tag libraries in JSTL and the following are JSTL tag Libraries
  • Core Tags
  • Formatting tags
  • SQL tags
  • XML tags
  • JSTL Functions
JSP Tag Libraries Artifacts

When we work with tag libraries we have three artifacts we need to consider
  1. Tag Library Description(TLD)
  2. Tag Library URI
  3. Tag Library Implementation JAR file
Tag Library Description(TLD)

Each tag library have its own Tag Library Description(TLD) file and its a xml file consist of tag information like tag name, tag attributes, what attributes are required and what are optional.

Tag Library URI

Tag Library URI is unique URL to identify the tag library,this will be used in JSP page to import tag libraries and we will use Tag Library URI in application deployment descriptor(web.xml) to integrate tag library with application. Tag Library URI is represented in TLD file.

Example:

<uri>http://alloy.liferay.com/tld/aui</uri>

Tag Library Implementation JAR file

Tag Library Implementation JAR file is a jsp tags implementation java classes make it as JAR file and we need to add this jar file in application class path that is application libdirectory or other server lib directory.

Usage of JSP Tag Libraries

We already know JSP tag libraries are used only in JSP pages. If we want use JSP tag Libraries in Web application JSP pages then we have to follow set of following steps.
  1. Register Tag Library in Application Deployment Descriptor(web.xml)
  2. Add Tag Library TLD file in WEB-INF directory
  3. Add Tag Library Implementation JAR file in Application Class Path
  4. Refer Tag Library URI in JSP page
Register Tag Library in Application Deployment Descriptor(web.xml)

First we need to register tag libraries in application web.xml file so that application can use tag in the JSP pages. When we register Tag libraries in web.xml we need to know tag library URI and Location of TLD file.

The following is registering tab libraries in web.xml file

<jsp-config>
<taglib>
<taglib-uri>http://liferay.com/tld/aui</taglib-uri>
<taglib-location>/WEB-INF/tld/aui.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/portlet_2_0</taglib-uri>
<taglib-location>/WEB-INF/tld/liferay-portlet.tld</taglib-location>
</taglib>
</jsp-config>

Note:

Configuration should be enclosed by <jsp-config/> tag. In the above configuration we have registered two tag libraries.

Add Tag Library TLD file in WEB-INF directory

We need to add Tag Library TLD file in the application WEB-INF directory. We can directly place in WEB-INF or we can create separate directory and we can place there. Make sure the path of TLD file that we have given in web.xml file to register tag library.

Example:

<taglib-location>/WEB-INF/tld/liferay-portlet.tld</taglib-location>

Add Tag Library Implementation JAR file in Application Class Path

Every tag library have its implementation java JAR file and this should be available in application class path that is WEB-INF/lib directory.

Example Implementation JAR files:

For JSTL (jstl-api.jar,jstl-impl.jar)
For Liferay(util-taglib.jar)

Refer Tag Library URI in JSP page

Its final step where we need to refer tag library in JSP page so that we can use respective tags in the JSP pages.

The following is the way to refer Tag Library in JSP page

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>

We need to give the tag library URI path that is available in TLD file and we will use prefix name to identify the tag library in the JSP page.

Example Usage of Tab Library Tags in JSP Page

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>

<portlet:actionURL var="sendUserNotification" windowState="normal" name="sendUserNotification">
</portlet:actionURL>

Note:

We will use prefix name to each tag notation and each tag may have attributes and we need to pass it in the tag.

Liferay JSP Tag Libraries

Apart from JSTL tag libraries Liferay also implemented their own tag libraries which we will use in liferay plugins development.

Liferay have many tag libraries for different contextual use and each tag libraries have collection of tags we can use in JSP pages to make it good presentation and rapid UI development.

The following are the Liferay Tag Libraries
  1. Portlet Tag Library
  2. Liferay Portlet
  3. Liferay Security
  4. Liferay AUI
  5. Liferay UI
  6. Liferay Util
Note:

Each tag library have many tags and its based on usage its made it as different libraries.

Usage Of Liferay Tag Libraries in Portlet Development

When we work with Plugin portlet development we extensively use the liferay tag libraries and its tags in the JSP pages.

Here interesting thing is Liferay Tag Libraries registering with portlet will be take care by the liferay auto deployment process so we need not to do manually registering the tag libraries.

Only we need to refer tag library URI information in the JSP page so that we can use tags in the JSP page.

The following is referring all Liferay Tag Libraries in the JSP page

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
<%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet" %>
<%@ taglib uri="http://liferay.com/tld/security" prefix="liferay-security" %>
<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
<%@ taglib uri="http://liferay.com/tld/util" prefix="liferay-util" %>

Example Usage of Liferay Tag from Portlet Tag Library in the JSP page

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>



<portlet:actionURL var="sendUserNotification" windowState="normal" name="sendUserNotification">

</portlet:actionURL>

The following is reference links to know each and every liferay JSP tag information

Note:

Each Library have many tags and that will be used in our portlet development.

How does Liferay Auto Deployment Process Registering the Liferay Tag Libraris in the Portlet plugin

When we create portlet from Liferay IDE there is no information in portlet web.xml file related to liferay tag libraries and no JAR file in portetl Lib directory and No TLD file in Portlet WEB-INF directory.

Once we deploy the portlet then portlet will be deployed in the application server deployment directory

If we use Tomcat then we can see portlet deployed in webapps directory(tomcat-7.0.42/webapps)

Observations:

Find your Poertlet in webapps directory and open web.xml file then you can see following configuration. Which shows that its registered all Liferay Tag Libraries

Example Path:

/tomcat-7.0.42/webapps/DockBarCustomNotification-portlet/WEB-INF/web.xml

<jsp-config>
<taglib>
<taglib-uri>http://liferay.com/tld/aui</taglib-uri>
<taglib-location>/WEB-INF/tld/aui.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/portlet_2_0</taglib-uri>
<taglib-location>/WEB-INF/tld/liferay-portlet.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://liferay.com/tld/portlet</taglib-uri>
<taglib-location>/WEB-INF/tld/liferay-portlet-ext.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://liferay.com/tld/security</taglib-uri>
<taglib-location>/WEB-INF/tld/liferay-security.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://liferay.com/tld/theme</taglib-uri>
<taglib-location>/WEB-INF/tld/liferay-theme.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://liferay.com/tld/ui</taglib-uri>
<taglib-location>/WEB-INF/tld/liferay-ui.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://liferay.com/tld/util</taglib-uri>
<taglib-location>/WEB-INF/tld/liferay-util.tld</taglib-location>
</taglib>
</jsp-config>

Now Open Portlet WEB-INF directory then you can see tld directory once you look into tld directory then you can see all liferay tag libraries tld files.

Example Path:

/tomcat-7.0.42/webapps/DockBarCustomNotification-portlet/WEB-INF/tld

Liferay Tag Library TLD files

aui.tld
liferay-portlet.tld
liferay-portlet-ext.tld
liferay-security.tld
liferay-theme.tld
liferay-ui.tld
liferay-util.tld

As we know each tag library have its own implementation jar file. Liferay also implemented java classes for all libraries and made it as one jar file called util-taglib.jarand it will be available in portlet lib directory(/tomcat-7.0.42/webapps/DockBarCustomNotification-portlet/WEB-INF/lib)

From above all observations we can understand that in the liferay portlet development we no need to do any manual configuration to register the liferay tag libraries and Liferay auto deployment processwill take all necessary configuration to make use of liferay tag libraries in portlet development that is in jsp pages.

Note:

Liferay Auto deployment process only resisted the Liferay tag libraries in the portlet not JSTL tag libraries.

Using JSTL Tag Libraries in Liferay Portlet Plugin

As we know liferay auto deployment is only configure the liferay tag librariesbut we also have many useful tag libraries that is JSTL tag libraries. When we want use JSTL tag libraries such as Core Tags ,Formatting tags ,SQL tags ,XML tags ,JSTL Functionsthen we are the responsible to register the JSTL tag librariesin the portlet development.

Assume we want to use JSTL core tag library

Use JSTL tag Libraries in the Portlet Development Using Liferay IDE

Liferay IDE provide very easy way to configure the JSTL tag libraries in Plugin Portlet development.

Go to your Plugin portlet in the eclipse project explorer

Double Click on “liferay-plugin-package.properties file its in portlet WEB-INF directory.

One we open this file it will be open in “properties” view mode as follows


You can see two inputs fields “Portal Dependency Jars” and “Portal Dependency Tlds” and we can also have Add buttons.

As we know when we work with tag libraries we need tag library implementation JAR file and Tag library TLD file.

jstl-api.jar, jstl-impl.jar” are the JAR files that was implemented the JSTL tag library and its have different TLD files like c.tld for JSTL core tags.

Now “Portal Dependency Jar” section click on Add buttonthen it will open dialog there you can find available JAR files list and search for “jstl-api.jar,jstl-impl.jar”files and select both files and then click on OK .

The following screen shows you adding JSTL jar files


Now “Portal Dependency Tlds” section click on Add buttonthen it will open dialog there you can have available JAR files list and search for “c.tld” file and select it then click on OK .

The following screen shows you adding JSTL TLD files


We have added JAR files and TLD file finally you can save the file(Ctrl+S).

The following screen shows added dependency jar files and dependency TLD files in the “liferay-plugin-package.properties

The following is source view of “liferay-plugin-package.properties

Liferay Plugin Portlet development when we need portal dependency jar files or other files like tld we will use “liferay-plugin-package.propertiesfile and the following is configuration.

portal-dependency-jars=\
jstl-api.jar,\
jstl-impl.jar
portal-dependency-tlds=c.tld

When we configure portal dependency jar files or tld files in “liferay-plugin-package.propertiesfile then liferay auto deployment process will copy specified JAR files and TLD files from Liferay Portal and Place into the Plugin Portlet appropriate locations like jar files will be placed in the portlet “WEB-INF/lib” directory and TLD files will be placed in the portlet “WEB-INF/tld” directory.

We have configured the JSTL tag libraries in our portlet, now we can use JSTL core tags in JSP pages .

The following is example code in JSP page to use JSTL Core tags

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:if test="<%= MicroblogsPermission.contains(permissionChecker, scopeGroupId, ActionKeys.ADD_ENTRY) && !userPublicPage %>">
<a>Add<p>
</c:if>

How Liferay Auto Deployment Process Configure JSTL Tag Libraries.

Once we add dependency jar files and dependency tld files in “liferay-plugin-package.propertiesfile then following things takes places after successful deployment of portler and these we can observed in deployed portlet in server deployment directory .
Go to tomcat webapps directory and find you pottlet then you can see following things

Observations:

Find your Poertlet in webapps directory and open web.xml file then you can see following configuration. Which shows that its registered all Liferay Tag Libraries

Example Path:

/tomcat-7.0.42/webapps/PortletConfigaration-portlet/WEB-INF/web.xml

<jsp-config>

<taglib>

<taglib-uri>http://java.sun.com/jsp/jstl/core</taglib-uri>

<taglib-location>/WEB-INF/tld/c.tld</taglib-location>

</taglib>

</jsp-config>

Now Open Portlet WEB-INF directory then you can see tld directory once you look into tld directory then you can see c.tld file

Example Path:

/tomcat-7.0.42/webapps/PortletConfigaration-portlet/WEB-INF/tld

As we know each tag library have its implementation jar file. JSTL implementation jar files called “jstl-api.jar, jstl-impl.jar” and it will be available in portlet lib directory(/tomcat-7.0.42/webapps/PortletConfigaration-portlet/WEB-INF/lib)

From above all observations we can understand that liferay portlet development when want add JSTL tag libraries we just need to specify the dependency jar files and dependency tld files in the “liferay-plugin-package.propertiesfile then Liferay auto deployment process will take all necessary configuration to make use of JSTL tag libraries in portlet development that is in jsp pages.

Manual Configuration to Use JSTL Tag Library in Plugin Portlet.

  1. Configure JSTL tag information in web.xml
  2. Add JSTL Implementation JAR file in Portlet lib Directory
  3. Add JST td file in portlet WEB-INF directory
  4. Refer Tag Library in JSP page 
 
Author
Share on Google Plus

About Meera Prince

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment