0

I am experimenting how JSF works and still a newbee. I am fetching data from my web api and would like to display the results on the UI upon page load. I am able to see the fetched data on the console but I would like to display this data in a div section. The returned data from the API is a JSON array. This is how I am currently doing inside a script tag of the page

function calledOnLoad() {
    var i = 0;
    fetch("http://localhost:1234/api/mydata")
       .then(response => response.json())
       .then(data => {
          console.log(data);
    })
    .catch(function() {
        console.log("error. Could not retrieve data");
    })
}

The above code would return the following table as Json array.

+-----------+-------------+-------+------+-------+
|        ID | Name        | Price | URL  | Count |
+-----------+-------------+-------+------+-------+
|         1 | Book        | 2     | url1 |     2 |
|         2 | Pen         | 1     | url2 |     1 |
|         3 | pencil      | 1     | url3 |     1 |
+-----------+-------------+-------+------+-------+

And i would like to display the values in a table on my page. I am successfully getting the data but i am having difficulties displaying this data.

<!DOCTYPE html>
<HTML lang = "en"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://xmlns.jcp.org/jsf/html" >
    
    <h:head>
        <title> Hello world</title>

    </h:head>
        
    <h:body onload= "calledOnLoad();">
    
        
    </h:body>
</HTML>
Jasper de Vries
  • 13,693
  • 6
  • 54
  • 86
Robert
  • 29
  • 6
  • Does this answer your question? [Invoke JSF managed bean action on page load](https://stackoverflow.com/questions/2451154/invoke-jsf-managed-bean-action-on-page-load) – Jasper de Vries Sep 29 '20 at 14:01
  • Not really because I am executing a JavaScript function which fetches data. In the examples they use java classes. Any other idea how to achieve this? – Robert Sep 29 '20 at 14:24
  • 1
    That's how you would load data in JSF. See also https://stackoverflow.com/questions/4421839/what-is-the-need-of-jsf-when-ui-can-be-achieved-with-javascript-libraries-such – Jasper de Vries Sep 29 '20 at 14:41
  • I will go through the examples and see how to incorporated that with my code. Thanks – Robert Sep 29 '20 at 14:59
  • Confusing question. It's hard to understand what "difficulties" exactly you're facing. You appear to want to fetch the data fully client-side instead of server-side. In this case it's in fact absolutely not different from when doing so in a plain vanilla HTML page. Just write JavaScript code exactly the same way as you would do when doing in a plain vanilla HTML page. JavaScript absolutely doesn't care about the type of the server side language who delivered the HTML document. Or are you actually asking how to do the job in server-side instead? In this case Jasper's hint is indeed correct. – BalusC Sep 30 '20 at 12:27
  • Sorry if my question is confusing. I am trying to accomplish the same task in server-side. Initially i wanted to do that using JavaScript but then it would be same as plain HTML page as you mentioned. I am going through some examples and the links @Jasber shared on how to do that. I am new to java just trying to experiment how this could be done server-side without using JavaScript. – Robert Oct 01 '20 at 08:33

0 Answers0