简体   繁体   English

将 servlet 响应发送到 html

[英]Send servlet response to html

I have a servlet that sends the response of string officeJson (it is a json response of having all the office details).我有一个 servlet,它发送字符串 officeJson 的响应(它是具有所有办公室详细信息的 json 响应)。 How can I retrieve the office details in html or by using Sightly?如何在 html 或使用 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?我如何在js文件中访问这个officeJson然后在html中使用它?

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

}

Everything I've tried has been unsuccessful.我尝试过的一切都没有成功。

HTL/Sightly is a server-side rendering template engine. HTL/Sightly 是一个服务器端渲染模板引擎。 It cannot interact with JS/AJAX as that's client-side.它不能与 JS/AJAX 交互,因为那是客户端。

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 .您可以以更加模块化的方式拆分您的 servlet 代码,以便您可以使用 Java Use-API 调用从 HTL 生成数据的方法。 You can then retrieve the data either from the return of the call.然后,您可以从调用的返回中检索数据。

For example:例如:

Java class ( MyHelper.java ): Java class ( MyHelper.java ):

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

HTL script ( myhelper.html ): HTL 脚本 ( myhelper.html ):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM