Skip to Content
avatar image
Former Member

Binding data SAPUI5 from Spring (JSON output)

Hi all,

I already created service (JSON output) with Spring and I want consume in SAPUI5. I want binding to view. Example: I have a service with url: http://localhost:8080/customer_data :

[{id: "1", name: "aaa"}, {id: "2", name: "bbb"}, {id: "3", name: "ccc"}]

I create model JSON in sapui5:

var model = new JSONModel("http://localhost:8080/customer_data");

I already success with this step. But I want load model with just "customer_data" because maybe I not place this code in localhost, maybe in server or other when I want to deploy. How to fix this problem? So, I want:

var model = new JSONModel("customer_data");

If I use code "new JSONModel(“http://localhost:8080/customer_data”);", is there any problem? Thanks.



Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

3 Answers

  • avatar image
    Former Member
    May 22, 2017 at 10:02 AM


    For creating jsonModel, try using this format "var oModel =new sap.ui.model.json.JSONModel()". It should work.

    For url issue, you will have to use correct URL address where you have placed your data or data file.



    Add comment
    10|10000 characters needed characters exceeded

    • Former Member


      Yes, I know it. but I have a problem when I load model, I want to load model just with separated url. example:

      var oModel = new JSONModel("customer_data");

      when I code above, I created model from url: localhost:8080/springproject/customer_data (this url is true, in a question is wrong, I forget put complete url). So, how to fix this problem? how to get ip and port in tomcat 7 with javascript? I not want directly to url: localhost:8080/springproject/customer_data for create the model.




  • May 23, 2017 at 07:02 AM

    considering both on the same server, you can form the service url dynamically as below:

    var appUrl = window.location; // for example :
    var protocol= appUrl.protocol;  //  'http:'
    var host = appUrl.hostname;  //  ''
    var port= appUrl.port;      //  8080
    var serviceUrl = protocol + "//" + host + ":" + port + "/customer_data";
    //now use this serviceUrl in your model.

    hope this is what you looking for.

    Add comment
    10|10000 characters needed characters exceeded

  • Jun 20, 2017 at 03:26 AM

    Hi, try this:

    sap.ui.getCore().setModel(new sap.ui.model.json.JSONModel(), "customerData")
    var oModel = sap.ui.getCore().getModel("customerData");
    oModel.loadData("http://localhost:8080/customer_data", this.async);
    //You'll need to set model to your view before binding it to any controls in that view.
    this.getView().setModel(sap.ui.getCore().getModel("customerData", "customerData");
    Add comment
    10|10000 characters needed characters exceeded