0

I have a Java class Controller.java. In the controller I can retrieve to MySQL database and keep the user informations into a list by List<User> users = userDAO.findAllUsers();

There is no problem so far.

To check, I can retrieve users from index.jsp by

  <c:forEach var="user" items="${users}"> 
      <li>${user.id}</li>
  </c:forEach>

I also have a myScripts.js file. I wrote scripts into it and I imported the file by

<script src="myScript.js"></script>.

There is still no problem so far.

I want to sketch a HighCharts line chart by using user list. It means, I have to retrieve a Java variable from a JavaScript function.

How can I do this? Thanks a lot.

bugrahan
  • 84
  • 2
  • 10

7 Answers7

1

Other way you can use org.json.JSONObject in your controller to return data into JSONObject and use

JSONArray jArray = new JSONArray();
JSONObject obj = null;
List<User> listUser = userDAO.findAllUsers();
int index = 0;
for (User user : listUser) {
     obj = new JSONObject();
     obj.put("id", index);
     obj.put("text", user);
     jArray.put(obj);
}

request.setAttribute("userJsonList", jArray.toString());

inside your html get this attribute using,

<input type="hidden" id="jsonData" value="${userJsonList}">

And then access the value from that element in javascript.

var jsonResponse = jQuery.parseJSON(document.getElementById('jsonData').value);

I think accessing the java object in javascript is not good instead if you are using java controller then JSONObject is perfect for you.

VPK
  • 3,010
  • 1
  • 28
  • 35
1

you have to get your array in java code.like this.

<% ArrayList<String> users = new ArrayList() %>

and push your user into users arraylist. then,

<script>
var user= new Array();
<% for (int j=0; j<users.size(); j++) { %>
    user[<%= j %>] = "<%= users.get(j) %>"; 
    <% } %>
</script>

Thanx.

0

There are tow ways.

  1. print your data in a js variable. var data = "${user.id}";

  2. give every <li> a id or class attribute. so you can retrieve the date via.

    var data1 = document.getElementByID().value;

    or jQuery

    $(#'').val();

Denis Kohl
  • 739
  • 8
  • 13
0

You cant get the parameter directly in Javascript.

Javascript is executed client side and JSP is executed server side.

using scriptlets,

var codesJS=new Array();
<% String[] codes=(String[])request.getAttribute("users");
if(codes!=null){
    for(int i=0; i<codes.length; i++){ %>
        var code='<%= codes[i] %>';           
        codesJS[<%= i %>]=code; 
    <%}
}%>

using el,

var data=${users};

but it is not recommended to mix scriptlets and java script . scriplets are discouraged over the decades ,see How to avoid Java code in JSP files?

it is better to use JSON

Hope this helps

Community
  • 1
  • 1
Santhosh
  • 8,181
  • 4
  • 29
  • 56
0

You could use a library to serialize your list into JSON. Or without additional libraries, you can spell it out explicitly like this:

var list=[
    <c:forEach var="user" items="${users}" varStatus="loop"> 
        ${user.id}<c:if test="${!loop.last}">,</c:if>
    </c:forEach>
];

You can extend this technique to include other properties:

var list=[
    <c:forEach var="user" items="${users}" varStatus="loop"> 
    {
        "id":"${user.id}",
        "name":"${user.name}"
    }<c:if test="${!loop.last}">,</c:if>
    </c:forEach>
];

Or you can include more information in data- attributes in he HTML and retrieve it from javascript later:

<c:forEach var="user" items="${users}"> 
    <li data-userid="${user.id}" data-name="${user.name}">${user.id}</li>
</c:forEach>

And in JS when you have a reference to the <li> in question:

var name=li.getAttribute("data-name");
CupawnTae
  • 14,192
  • 3
  • 29
  • 60
0

You are only bringing through the user ids at the moment. When you iterate the user list in jsp, include the data you want to plot. e.g. (If there was a public 'getAge()' method on your user class):

    <c:forEach var="user" items="${users}"> 
        <li>${user.id}</li>
        <input type="hidden" id="${user.id}" value="${user.age}" />
    </c:forEach>
ne1410s
  • 6,864
  • 6
  • 55
  • 61
0

If you are using jsp pages you can also use c:out
for example : <c:out value="${user.id}"/>
This should work for you

Bhargav Mehta
  • 389
  • 7
  • 18