Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Calling Custom Utility Methods in JSP

Avatar

Level 2

I am currently trying to call a custom java utility function that I wrote in the bundle section of my cq5 code. I am calling this from a components jsp.

In my common bundle I have a utility class under src/main/java. My package is something like com.test.utility and my file is jcrUtility.java. Inside the java file I have something like:

public static ArrayList<String> propertyToArrayList(Node n) { ... }

In my component's jsp file I have something like this:

<% page import="com.test.utility.jcrUtility"%> <% ArrayList<String> propList = jcrUtility.propertyToArrayList(currentNode); %>

In both my java class and my jsp file I have the correct other libraries imported such as java.util.ArrayList, javax.jcr.Node, etc. I have also compiled my OSGI bundle and updated it in the Bundles web console.

The current exception I am receiving is as follows:

org.apache.sling.api.scripting.ScriptEvaluationException: org.apache.sling.scripting.jsp.jasper.JasperException: Unable to compile class for JSP: The method propertyToArrayList(Node n) is undefined for the type jcrUtility

Also, I have checked and my bundle is "Active" and my package is in the "Export Package". mported.

I am not sure what the problem is considering the java in my utility class and the java in my jsp looks sound. Any help would be appreciated.

1 Accepted Solution

Avatar

Correct answer by
Level 10

Few things look funny about this. 

First - i would build the backend OSGi service using @component and @service annotations. You can use Maven too and define a Java Interface:

package com.test.utility;
 
public interface jcrUtility {
      public static ArrayList<String> propertyToArrayList(Node n)
 }

Then define an implementation class in the same package:

//This is a component so it can provide or consume services
@Component
   
@Service
public class jcrUtilityImpl implements jcrUtility{

//implementation code

}

Build and deploy - build via Maven.

Then in your JSP code that invokes the service, use sling.getService().

com.test.utility.jcrUtility jUtilService = sling.getService(com.test.utility.jcrUtility.class);

jUtilService.propertyToArrayList(node);

Now - we have all this information in an end to end community article  - including how to pass nodes. See:

Passing JCR Node Objects to Adobe Experience Manager Custom Services

View solution in original post

1 Reply

Avatar

Correct answer by
Level 10

Few things look funny about this. 

First - i would build the backend OSGi service using @component and @service annotations. You can use Maven too and define a Java Interface:

package com.test.utility;
 
public interface jcrUtility {
      public static ArrayList<String> propertyToArrayList(Node n)
 }

Then define an implementation class in the same package:

//This is a component so it can provide or consume services
@Component
   
@Service
public class jcrUtilityImpl implements jcrUtility{

//implementation code

}

Build and deploy - build via Maven.

Then in your JSP code that invokes the service, use sling.getService().

com.test.utility.jcrUtility jUtilService = sling.getService(com.test.utility.jcrUtility.class);

jUtilService.propertyToArrayList(node);

Now - we have all this information in an end to end community article  - including how to pass nodes. See:

Passing JCR Node Objects to Adobe Experience Manager Custom Services