简体   繁体   中英

How to pass JSON array from Struts 2 action class to jQuery?

I creating a Java web application with an action file which generates data from database to a JSON array. Now, I want this JSON array to be passed to jQuery? How is that possible?

FYI. I applied Struts 2, Spring and Hibernate frameworks. I am not using PHP for this app.

Update:

This is my Struts 2 action method:

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;
}

And this is my jQuery. I am using BootstrapTable so this is the structure, and I want the value of jsonData to be passed to this jQuery:

$('#record').bootstrapTable({
    method : 'get',
    data: jQuery.parseJSON(VALUE_OF_jsonData_HERE),
    cache : ...
    ...
    ...
});

Using Ajax will help you,

refer this to get started, also do read about Ajax first to have a background

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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