我使用一个动作文件创建了一个Java Web应用程序,该文件从数据库生成数据到JSON数组。现在,我想把这个JSON数组传递给jQuery?这怎么可能?如何将JSON数组从Struts 2动作类传递给jQuery?
仅供参考。我应用了Struts 2,Spring和Hibernate框架。我没有使用这个应用程序的PHP。
更新:
这是我的Struts 2的操作方法:
public static String jsonData = null;
public String generateJson() throws Exception
{
JSONObject json = null;
JSONArray jsonArray = new JSONArray();
this.records = this.recordManager.getAllRecords();
for (RecordEntity record : records)
{
json = new JSONObject();
json.put("id", record.getId());
json.put("firstname", record.getFirstName());
json.put("lastname", record.getLastName());
json.put("due", record.getDue());
json.put("email", record.getEmail());
json.put("website", record.getWebsite());
jsonArray.put(json);
}
System.out.println(jsonArray.toString());
jsonData = jsonArray.toString(); // jsonData will be passed to jQuery
return SUCCESS;
}
这是我的jQuery。我使用BootstrapTable所以这样的结构,我想jsonData的值传递给这个jQuery:
$('#record').bootstrapTable({
method : 'get',
data: jQuery.parseJSON(VALUE_OF_jsonData_HERE),
cache : ...
...
...
});
如果servlet在响应输出中打印JSON编码的数据,则您只需通过常规的ajax请求获取它即可['$ .getJSON'](http://api.jquery.com/jQuery.getJSON/) – 2014-12-06 15:29:34
如何?抱歉,我没有提及,我是servlets和Struts 2的新手。 – Dylan 2014-12-06 15:31:56
也可以直接在页面中将json输出到javascript变量中。方法在某种程度上取决于它的使用方式 – charlietfl 2014-12-06 15:32:40