How to create a tag-library in Liferay 7.2?

1.3k Views Asked by At

I am new to Liferay and I need to create a custom tag-library that I can use in my liferay portlets. But the probem is, I am not getting any step-by-step tutorial or guide to create and use it in Liferay.

The first problem is that, I am unable to figure out what kind on project I need to create for this.

For Ex. If I am creating a portlet, I need to use the MVC-PORTLET project template while creating the new liferay module. In the same way, what template I can use for creating custon Tag-Libraries in Liferay.

Thereafter I need to know the remaining steps for creating a tag and using it in my portlets.

1

There are 1 best solutions below

2
Milen Dyankov On

Tag libraries are not Liferay Portal specific modules. They are standard technology which was (is?) part of Java EE spec. The last official tutorial I know of that covers them is The Java EE 5 Tutorial. You should probably read that to learn how to build custom tag libraries. How you use them in Liferay Portal is no different than how you use them in any other web application.

Keep in mind JSP and thus Tag Libraries are rather old technologies. While still widely used in many projects (like Liferay Portal), I think they are replaced by JSF in recent Java/Jakarta EE specs. Moreover with the web evolution in the last few years many developers and products (like Liferay) are moving away form backend UI generation to things like Web Components and modern JS frameworks.

UPDATE (to address the clarification in the comment):

There are no templates (that I'm aware of) in Liferay's tooling for building taglib bundles. But a standard OSGi module should do the job. The important part is to tell that you provide the jsp.taglib capability in your bnd.bnd file:

Provide-Capability:\
    osgi.extender;\
        osgi.extender="jsp.taglib";\
        uri="http://YOUR_DOMAIN/tld/lib";\
        version:Version="${Bundle-Version}"

Make sure to also export the relevant packages. You can check how other Liferay taglibs are doing it. For example the asset-taglib.

On the consumer bundle side, you have 2 options:

  1. If you are using Liferay's workspace (or relevant BND plugins) you can simply add
   -jsp: *.jsp,*.jspf

instruction in your bnd.bnd file. That will tell trigger a BND plugin that will generate the needed requirements.

  1. If you don't use that tooling you will need to add the requirement manually:
Require-Capability:\
    osgi.extender;\
        filter:="(&(osgi.extender=jsp.taglib)(uri=http://YOUR_DOMAIN/tld/lib))"