I have been struggling with using POJO in my javascript transferred by the server. Javascript doesnt seem to understand the fields of the object.
Is there a way i can work around this.
Thanks in advance!
EDIT : I was under the impression that I would probably be able to access the primitive fields of the POJO in javascript but seems like that cannot be done. Forgive my ignorance. I would like to now ask if there is a good library that does the conversion from POJO to JSON and vice versa.
I have been struggling with using POJO in my javascript transferred by the server. Javascript doesnt seem to understand the fields of the object.
Is there a way i can work around this.
Thanks in advance!
EDIT : I was under the impression that I would probably be able to access the primitive fields of the POJO in javascript but seems like that cannot be done. Forgive my ignorance. I would like to now ask if there is a good library that does the conversion from POJO to JSON and vice versa.
Share Improve this question edited Sep 16, 2011 at 4:45 kshitij asked Sep 15, 2011 at 19:25 kshitijkshitij 1553 silver badges10 bronze badges 3- 3 you should add some details, like how you are requesting the data, what the server returns, and what you are doing in the client once you get the response. – hvgotcodes Commented Sep 15, 2011 at 19:31
- JavaScript is unrelated to Java. They have intentionally similar syntax but there is no reason code of one type would work in another type's environment. – Erik Reppen Commented Sep 15, 2011 at 20:12
- I was under the impression that I would be able to access the primitive fields of the POJO in javascript but seems like that cannot be done. Forgive my ignorance. I would like to now ask if there is a good library that does the conversion between POJO to JSON and vice versa. – kshitij Commented Sep 16, 2011 at 4:43
5 Answers
Reset to default 4You can't "use POJOs in javascript"
You should have your javascript request some data from the server via ajax. The server responds with the data, typically in Json. You then convert the json into javascript object literal(s).
Have a look at this :
http://p2p.wrox./j2ee/16393-calling-java-method-via-javascript.html
It is to call a java method.
Well you can't access the objects directly. You will need ajax call to servlet to do so.
You might be interested in the DWR (Direct Web Remoting) project. You'll still need to clear up your misconceptions about how web apps work, and the difference between client and server side behavior/code, though. DWR can, at least, make it seem more like the (wrong) way you think about it now.
If you just need to convert data between POJO and JSON in java environment consider using Jackson or google-gson.
you can use POJO objects in java side and you can use extra library for converting POJO objects to json object. and than used this objects like POJO.
look at : Jersey library
or you can check xstream lib: for json example : http://x-stream.github.io/json-tutorial.html for xml example : http://x-stream.github.io/tutorial.html
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745596598a4635154.html
评论列表(0条)