Codebay Innovation
CodeBay Coding

AEM and JSP Custom Tag Library

Today we just want to show how to create  and use a custom tag library in AEM in a simple 3 steps walkthrough. We don’t want to get into the general details of how to actually write a JSP Custom Tag Library. For more information on writing JSP Custom Tag Libraries, you can see the JavaEE 5 Tutorial – Custom Tags in JSP Pages documentation.

Why creating  a custom tag library?

One of the best practice on how to write jsp, is do not mix presentation logic with business logic. This should mean to minimise or do not use scriptlets at all. Instead you should use a combination of EL expressions and Custom Tag Libraries (including the JSTL).

Sometimes scriptlets are just faster for reaching your goal and we are not purist to say never use scriplets, just try to find your right balance in coding without adding a lot of business logic inside your components jsp.

Step 1. Add your Tag Library Descriptor

Create your Tag Library Descriptor file inside of the META-INF directory within your OSGi Bundle (e.g. my-custom-tag-library.tld).

IMPORTANT: In your Tag Library Descriptor (.tld file), for the value of the tag, you must specify a full and unique URL to identify your Tag Library within the CQ environment. If you only specify a relative URI, CQ often fails to find your library.

Step 2. Create your Tag Library class

Just create your tag library class as normal.

Step 3. Use your custom tag library from jsp

Once you have deployed your custom tag library into your AEM instance, you can just  use it into your components jsp with the standard mechanism.

Do you need help for you AEM projects?

You Might Also Like

No Comments

    Leave a Reply