1

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

}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
gallea01
  • 82
  • 1
  • 8
  • Why JSON in particular when you can achieve ajax things using third party libraries like Richfaces, Primefaces etc..,? – mvg Mar 03 '14 at 14:54
  • Actually, the reason why I'm trying to output it as a JSONObject is that I want to integrate it with d3.js. Does this make sense? – gallea01 Mar 04 '14 at 09:17
  • Yes you do. Just asked out of curiosity! – mvg Mar 04 '14 at 10:06
  • Why don't you just use a collection of javabeans or maps? Or do you have the `JSONObject` already at hands from elsewhere? Then just convert it to a collection of javabeans or maps, so that you can provide exactly the model the view needs. – BalusC Aug 13 '14 at 06:06

1 Answers1

1

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()

Daniel
  • 36,833
  • 10
  • 119
  • 200