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.

How can I call a backend method of a java model from the htl frontend?

Avatar

Level 5

Hello, what happens is that I have a template that is for products and the information is brought from AiO, but what I want to do now is that in the template we want to implement a button that when clicked, create a pdf and download it.

 

For it in the java model of the template I have created a method that contains the structure and design of the pdf using the library IText7, since, the pdf will be different to the design of the template.

 

My question here is, how can I do so that in the frontend of the htl when I click on the download button I call the java backend to the method that creates the pdf and download it from the template model?

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

6 Replies

Avatar

Community Advisor

@Aaron_Dempwolff you cannot make a call to sling model by button click, thats more of client side interaction but you can make a call to servlet(JS Ajax call)and get the return of pdf url it generated and take the user to that pdf in same tab or new tab. Just an idea.

But my method that creates the PDF is in java, it is not in javascript, can I still make the call?

 

And if so, do you have any guide or tutorial on how I could create a servelt for my java method and how I can send it to call in the frontend when I click on the button?

 

Also I have another question, if I use a servelet how would I send the information of my template model (product name, product image, description, etc) to the servelet?

Avatar

Community Advisor

@Aaron_Dempwolff , You can consider creating a servlet registered to the component resource type. This servlet should be able to serve the PDF by creating it using the mentioned PDF library. 

Avatar

Level 5

Do you have any guide or tutorial on how I could create a servelt for my java method and how I can send it to call in the frontend when I click on the button?

 

Also I have another question, if I use a servelet, how would I send the information of my template model (product name, product image, description, etc) to the servelet?

Avatar

Community Advisor

I agree with @Saravanan_Dharmaraj  and @Harwinder-singh , you need to use a servlet instead.

 

Do the following:

1. Create a servlet that has the functionality to create the PDF (this is Java) Example: https://medium.com/@toimrank/aem-sling-servlet-ca9e5926a384 

2. Once you click the button, you will use Javascript and through an AJAX call, you will invoke the servlet. Example: https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/how-to-call-servlet-from-l... 

 

Here is a full example of both parts working together: https://www.youtube.com/watch?v=iQu65qNN428 



Esteban Bustamante

Nice, also I have another question, if I use a servlet, how would I send the information of my template model (product name, product image, description, etc) to the servlet?