gallea01
gallea01

Reputation: 82

How to present a JSONObject in a JSF page

I am trying to present a JSONObject in a table in my Facelets file. How can I achieve this?

package com.myportal.dashboard;

import javax.annotation.PostConstruct;
import net.sf.json.JSONObject;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;

@Component
@Scope(value=WebApplicationContext.SCOPE_SESSION)
public class DashboardBacker {

    private JSONObject dashboardTable;

    @PostConstruct
    public void initializeTable() {
        dashboardTable.put("Completed", 26);
        dashboardTable.put("Failed", 33);
        dashboardTable.put("In Progress", 44);
        dashboardTable.put("On-hold", 9);
    }

    public JSONObject getDashboardGraphs() {
        return dashboardGraphs;
    }

}

Upvotes: 1

Views: 2714

Answers (1)

Daniel
Daniel

Reputation: 37061

Turn you json object into string and set it into beans property , like this:

String myJson = null; //getter / setter

@PostConstruct
public void initializeTable() {
    ....
    myJson = dashboardGraphs.toString();
    //myJson = new Gson().toJson(dashboardGraphs); //in case of Gson usage
}

than in your xhtml

Then in your js code access it using that input id $('#my-json-holder').val()

Upvotes: 1

Related Questions