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

时间:2017-03-31 07:36:48

标签: javascript json wicket

这是我的java代码

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

这是我的javscript文件

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

2 个答案:

答案 0 :(得分:1)

您需要使用Component或Behavior的#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));
    }
}

答案 1 :(得分:0)

//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