1

I have a question: I have a java object that's a List<User> and I want pass it to javascript for populate the source of a jQuery autocomplete. From jsp I pass the object present in the session by this tag

<input type="hidden" id="all_users" value=" < s: property value="#session.allUsers" /> " />

Then in javascript

var allUsers = $('#all_users').val().replace('[','').replace(']','').split(',');

Now I don't know what's next step because I obtain value as bean.User@4a255c. How I can convert this object for manage it in javascript and obtain the values that contains?

5
  • 1
    You could use JSON. For instance, use Jackson to serialize your List<User> to a JSON array, then parse it using JavaScript's builtin JSON capabilities to make it an array.
    – fge
    Commented Jun 14, 2013 at 15:03
  • Have you tried using JSON.parse on the allUsers value?
    – user2417483
    Commented Jun 14, 2013 at 15:04
  • How big is the list? Is it required that you have the data in the client page, or is Ajax enough? Is the JS external to the JSP? Commented Jun 14, 2013 at 15:41
  • The list could be big and has 3 field for every item. Now I tried JSON and the conversion of java object in json object is ok, but when I pass the value in the jsp through this tag: <input type="hidden" id="all_users" value="<s:property value="#session.allUsers"/>"/> in javascript the parsing of that value not work because it's a unknow object... Here I show how I wrote: var allUsers = $('#all_users'); var array = JSON.parse(allUsers); where is the problem? Commented Jun 15, 2013 at 14:40
  • Problem risolved, I forget of call method .val() var allUsers = $('#all_users').val(); var array = JSON.parse(allUsers); Now is Ok, JSON is very powerful! Commented Jun 15, 2013 at 14:55

1 Answer 1

0

I do not have experience with struts but you may try something like the following:

<input type="hidden" id="all_users" value=" < s: property value="#Arrays.toString(session.allUsers.toArray())" /> " />
2
  • And yes I agree with the comments, that JSON would be a better alternative. Commented Jun 14, 2013 at 15:14
  • Can someone give me some example or guide of how I do it with JSON? Thanks Commented Jun 14, 2013 at 19:14

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.