Search code examples
javajspjstluriaem

Java Function with No Return in TLD


So, I'm trying to figure out how to interact with a Java function from JSP using JSTL and a TLD. This particular function doesn't have a return, so I'm assuming I need to call it in the JSP straight up with an EL call:

<html code here>
${prefix:createChildPage(currentPage, resourceResolver, strings, string)}
<other jstl code here>
<more html here>

But I cannot figure out how to set up the TLD properly for this. The Java function looks like this:

public static void createChildPage(Page page, ResourceResolver resourceResolver, String[] strings, String string) throws Exception {
    // code in here
}

As you can see, there is no return. The function just takes its input arguments and uses those to build out a child page structure inside an Adobe CQ/AEM environment using JCR/Oak node structure. So, I tried to set up the TLD function like this, and it keeps failing telling me that the method doesn't exist in my class.

<function>
    <name>createChildPage</name>
    <function-class>com.org.utils.ClassName</function-class>
    <function-signature>createChildPage(com.day.cq.wcm.api.Page, org.apache.sling.api.resource.ResourceResolver, java.lang.String, java.lang.String)</function-signature>
</function>

At first, I was thinking it might be that I just don't know how to classify a String[] for the TLD. But I've tried so many classes with the same result, that I'm fairly certain it might have something to do with the fact that I'm not specifying a return. For the example code above, I've classified the String[] as java.lang.String, but maybe its supposed to be java.util.Array. I've run my head into a wall and cannot really tell at this point. And I'm assuming that I need the TLD function for it since I have to have some way of calling from the JSP (hence, the ${prefix: part of the code.

So my question is this: how do I call a Java function from JSP using JSTL/EL that doesn't have a return? It just does something when the page is loaded.


Solution

  • I ended up calling the function from another function that returned the path to the created page.

    public static String getArticleIncludePath(Page givenPage, String pageName, ResourceResolver resourceResolver) throws Exception {
        // Call createChildPage to build out child page as needed
        createChildPage(givenPage, resourceResolver, pageName);
        // Now get the child includePath
        String childPath = givenPage.getPath() + pageName;
        return childPath;
    }