2

My project is in older technology it is using JSP/servlets, we are to migrate it to spring boot, as part of this we are migrating some pages to angular js implementation. In a particular page there is a JSON object from servlet that is coming to JSP, the servlet code is like

JSONObject vReportDetails = new JSONObject();
inRequest.setAttribute("REPORT_DATA", vReportDetails);

I want to read this data in angular js controller, right now they are using

<% JSONObject jObj = (JSONObject) request.getAttribute("REPORT_DATA"); %>

and reading the JSON data

please help me with reading this data in a angular js

Thanks in Advance

1 Answer 1

2

UPDATE So JSP is already loading the HTML page with the data embedded. Angular is just a client-side framework so it can either do an HTTP-GET but you probably don't want the data as a public endpoint. The best way I can think around this is to go through the DOM

$scope.angularVariable = document.getElementById("WhereTheJsonIs");

Note that I am not sure how the json is being loaded in the DOM, but the main idea is the JSON is already loaded on the page and using JavaScript you can get that data from the DOM and set it to an Angular variable.


So JSON stands for JavaScript Object Notation and what you are asking is not an "Angular" question as much as a JavaScript question. Angular just being a framework that lets you do much more stuff.

So I can set a variable to be the JSON object like

var theJSON = { "firstKey" : "value" };

and since Angular is just JavaScript you can do the same, (assuming you are using Angular v1 and not v2) you probably are using $scope as your way to set the variable which is fine, same rules apply for a normal JavaScript variable

$scope.theJSON = { "firstKey" : "value" };

The moral of the store

other languages have classes to handle JSON as its not natural to languages like Java, C++, etc. but since you are working with JavaScript you can use JSON as JSON because it was made to be used for JavaScript and remember that Angular is just JavaScript underneath.


If you are using a HTTP request the response is already in JSON so you can set that a variable

var theJSON; 
$http.get('/myEndPoint').then(function(response) {
     theJSON = response;
});
Sign up to request clarification or add additional context in comments.

7 Comments

My main doubt was what to mention in /myEndPoint ??
This is a server question... Angular is just the front-end. It will load the page and thats it. The idea is if this JSON is on the server you need to request it... where is this JSON data your looking for?
its comes from the servlet which will call the JSP.... right now in JSP it is read as <% JSONObject jObj = (JSONObject) request.getAttribute("REPORT_DATA"); %>
So looking at this I think there is a structual issue combining the two ideas en.wikipedia.org/wiki/JavaServer_Pages#/media/… This shows the old Model-View-Controller layout angularjstutorials.net/img/angularJs_mvc_framwork.jpg Shows how Angular does it So what I am trying to understand is this JSON data is somewhere on the server side, you might need to create the /endPoint in servlet to grab it from Angular
ok, so I totally misunderstood the part where JSP is just a render engine... so JSP already creates an HTML page and "embeds" the data when the page loads, Angular will need to access it through the DOM, check above for the edit to answer
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.