Abhinay Gidugu
Abhinay Gidugu

Reputation: 1

Send servlet response to HTL/Slightly

I have a servlet that sends the response of string officeJson (it is a json response of having all the office details). How can I retrieve the office details using Sightly? I need a proper explanation with some code.

request.setAtrribute("officeDetail",officeJson);
response.setContentType("Application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(officeJson);`

How can i access this officeJson in js file and then use it in html?

{
"officeSummary":{
   "officeName": abc,
    "officeId":124,
    "officeAddress":{
        "country":us,
        "state":niantic,
        }
   "phoneNumber":986542160
   }

}

Everything I've tried has been unsuccessful.

Upvotes: 0

Views: 646

Answers (1)

Vlad
Vlad

Reputation: 10780

HTL/Sightly is a server-side rendering template engine. It cannot interact with JS/AJAX as that's client-side.

You could split your servlet code in a more modular way so you can call the method(s) that generate the data from HTL with the Java Use-API. You can then retrieve the data either from the return of the call.

For example:

Java class (MyHelper.java):

public class MyHelper {
    public OfficeSummary getOfficeSummary() {
        return MyStaticService.getOfficeSummaryData();
    }
}

HTL script (myhelper.html):

<sly data-sly-use.helper="${MyHelper}" data-sly-set.officeData="${helper.officeSummary}">
    ${officeData.officeName}
</sly>

Upvotes: 3

Related Questions