May 3, 2020
Estimated Post Reading Time ~

How to Trim Whitespaces in AEM

As an AEM developer, I'm always looking for ways to make my life easier and my screen tidier and if that benefits the end user as well, then it's a win-win situation. So whilst working in Zen Garden (on AEM) recently, I decided to trim the whitespaces on the website I was creating, packing the html source and giving me much prettier code that was easier to read. This is something that I've done many times before but I found that in AEM 6.1 it requires a different approach which I thought I'd share.

In older versions of AEM/CQ you would have to find the Apache Sling JSP Script Handler configuration in the OSGi Console. Then you would click the checkbox labelled "Trim Spaces" and clear all compiled jsp files under /var/classes.



However, in AEM 6.1, website source files are no longer stored under /var/classes node and they cannot be removed so easily. So my first thought was to recompile the jsp files using a tool from the OSGI console called Apache Sling JSP Scripting.



Unfortunately, that still had no effect. It transpired that this button only recompiles jsp files that are present on the AEM instance, so, JSP's from our components stored in /apps, for example.

The solution lay elsewhere - according to this you can find compiled jsp files right where the instance is located in the filesystem and not on the instance itself as before. To do this, you have to find the Apache Sling Commons FileSystem ClassLoader (org.apache.sling.commons.fsclassloader) bundle and write down its number (see demo 1 below). Then go to the filesystem on your instance and find the following folder:

[AEM_INSTALL_DIR]/crx-quickstart/launchpad/felix/bundle[BUNDLE_ID]/data/classes.

Where:
- AEM_INSTALL_DIR is the directory where the AEM instance is located.
- BUNDLE_ID is the previously noted FileSystem ClassLoader bundle name.

After deleting all class files there (see demo 2 below), the only thing that remains is to restart the Apache Sling Commons FileSystem ClassLoader bundle. Once this is done, files will be stored with the updated configuration - and trim whitespaces is enabled.



Also below, you'll notice the jsp.config file. This file contains the current configuration set in Apache Sling JSP Script Handler. After every edit you will see the change in this file.



So I hope that if you, like me, prefer to work in a tidier space and make page loading a little faster for end-users at the same time, you'll find this tip useful.



By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.