Persisted Selectors

Apache Sling defines Selectors as: ” If the first character in the request URL after the resource path is a dot (.), the string after the dot up to but not including the last dot before the next slash character or the end of the request URL comprises the selectors. If the resource path spans the complete request URL no selectors exist. If only one dot follows the resource path before the end of the request URL or the next slash, also no selectors exist.”

Case Study

You might have used selectors many times in passing on parameters to a servlet or to identify different behaviors of a component using Sling Resource Resolution. Often there is a need to persist a selector of one page throughout the website which forces us to modify all existing links. Generally, such requests are handled by a JAVA class where all links are modified using that selector and the experience is persisted.

Another interesting approach to do so is using Cookie + Apache configurations if we don’t want to modify all the links on a website.Let’s say for one of your campaigns, there is a selector X on a page. Now based on that selector component behavior is changed and I need that experience to persist even if the user goes to multiple pages on the site and come to the first page.

When a user will land on that page you will set a Cookie (Let’s say “cookieX”) with that selector value.Do make sure you accept only valid Selector values and don’t create Cookie for any selector value. You can make this Cookie as either a Persisted (Will stay for a specified duration ) or Session (Will stay till the browser is Open) Cookie based on your use case and duration of the campaign.

Next, whenever this Cookie is present on the Client side, the following Apache Rule will persist this selector –

Blog Hits

Author

I am Hashim Khan, a certified AEM developer ,a JAVA enthusiast, an expert Consultant and a Web Development Solution Provider . If you have a project that needs some AEM expertise then that’s where I come in! Since the start of my career I have been working on AEM and related technologies & since Feb 2015 , through this blog I am trying to share my AEM knowledge and give back to the AEM community.