How to add custom Sling binding?

I want to extend Sling bindings with a custom object, so it will be available in all JSP files without extra effort. I implement an BindingsValuesProviderOSGi service like this (this is not real code, but quite similar):

@Component
@Service
public class ContentBranchBindingProvider implements BindingsValuesProvider {

    @Override
    public void addBindings(Bindings bindings) {
        final Resource resource = (Resource) bindings.get("resource");
        final String[] splitPath = StringUtils.split(resource.getPath(), '/');
        bindings.put("contentBranch", splitPath[1]);
    }
}

I expect the binding to contentBranchbe available as a scripting variable in JSP:

<%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
<%@include file="/libs/foundation/global.jsp"%>
Your content branch is: ${contentBranch}

However, the above JSP does not output the content branch, but:

Branch of your content:

I used a debugger to see that my method addBindings()is being called and putting the correct value on the map bindings. What can I do to make it available as ${contentBranch}in JSP?

+4
source share
1

Sling . , , Sling .

Sling <sling:defineObjects/>, , , , , contentBranch.

, <sling:defineObjects/>, bindings, :

<%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
<%@include file="/libs/foundation/global.jsp"%>
Your content branch is: ${bindings.contentBranch}

defineObjects.

, Sling Sightly - :

Your content branch is: ${contentBranch}
+4
source

Source: https://habr.com/ru/post/1570313/


All Articles