如何在 apache wicket 中将 json 数据发送到 javascript?

How to send json data to javascript in apache wicket?

这是我的java代码

     Gson gson = new Gson();
     json = gson.toJson(applicationsList);
     System.out.println(json);

这是我的java脚本文件

       <html>
       <script>here i will use json data </script>
       </html

您需要使用组件或行为的#renderHead() 方法。阅读 https://ci.apache.org/projects/wicket/guide/7.x/single.html#_adding_resources_to_page_header_section 了解更多信息。

public class MyComponent extends Component{

    @Override
    public void renderHead(IHeaderResponse response) {
       String json = ...;
       response.render(JavaScriptHeaderItem.forScript(json));
    }
}
//We need to call a fetch function in the script tags fam

<script>
  fetch('localhost:8080/getMyJson', (res, err) => { 
    err ? console.err(err) : console.log(res) 
  })
</script>

// the default HTTP method is set as GET