admin管理员组文章数量:1294039
I am using Ajax in my JavaScript and that sets an array of objects of following class in a response:
public class RetrieveTempSecVersions {
private String templateName;
private int[] versionNumber;
public String getTemplateName() {
return templateName;
}
public void setTemplateName(String templateName) {
this.templateName = templateName;
}
public int[] getVersionNumber() {
return versionNumber;
}
public void setVersionNumber(int[] i) {
this.versionNumber = i;
}
}
using:
aoRes.setContentType("text/xml");
aoRes.setHeader("Cache-Control", "no-cache");
aoRes.getWriter().write(template[]);
Now I want to read all the elements from template array and its details into my JavaScript.
Can anyone help me in how to do that?
I am using Ajax in my JavaScript and that sets an array of objects of following class in a response:
public class RetrieveTempSecVersions {
private String templateName;
private int[] versionNumber;
public String getTemplateName() {
return templateName;
}
public void setTemplateName(String templateName) {
this.templateName = templateName;
}
public int[] getVersionNumber() {
return versionNumber;
}
public void setVersionNumber(int[] i) {
this.versionNumber = i;
}
}
using:
aoRes.setContentType("text/xml");
aoRes.setHeader("Cache-Control", "no-cache");
aoRes.getWriter().write(template[]);
Now I want to read all the elements from template array and its details into my JavaScript.
Can anyone help me in how to do that?
Share Improve this question edited Apr 3, 2012 at 22:42 рüффп 5,44834 gold badges73 silver badges121 bronze badges asked Feb 25, 2011 at 8:45 VarunVarun 5,06112 gold badges56 silver badges86 bronze badges3 Answers
Reset to default 3Have you considered using Google Web Toolkit? With GWT you have a number of choices for sending objects between client and server, including GWT RPC, RequestFactory, and JSON.
You need to transform your array of Java objects into something JavaScript understands. There are two obvious choices : XML (AJAX means Asynchronous Javascript And XML), or JSON. JSON is probably easier and more lightweight.
All the AJAX JavaScript libraries (JQuery, etc.) have good support for JSON. See http://api.jquery./jQuery.getJSON/ for example.
There are also several Java APIs to transform Java objects into JSON (look at GSON for example).
You'll have to modify the content type of the response : it's not text/html, but application/json if you use JSON.
As already said jQuery is very good for ajax manipulation.
Another good framework I liked to use was DWR: https://github./directwebremoting/dwr
It is quite lighter than jQuery (because only focused on Ajax calls and not ponents) and very easy to understand.
本文标签: Handle java object in javascriptStack Overflow
版权声明:本文标题:Handle java object in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741593699a2387283.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论